电商网站部署后模板渲染及数据库关联错误求助
电商站点部署后两类错误的排查与解决
一、登录/注册提示customuser不存在
问题描述
部署后登录或注册时提示customuser不存在,但已定义CustomUser模型,本地运行正常:
class CustomUser(AbstractUser): is_customer = models.BooleanField(default=False) is_merchant = models.BooleanField(default=False) is_superuser = models.BooleanField(default=False) first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255)
排查与解决
- 确认用户模型配置:检查项目
settings.py中是否配置AUTH_USER_MODEL,格式为AUTH_USER_MODEL = '你的应用名.CustomUser'(例如CustomUser在projectapp下则写AUTH_USER_MODEL = 'projectapp.CustomUser'),确保部署环境配置与本地一致。 - 执行数据库迁移:在部署服务器运行以下命令,同步模型变更到数据库:
首次部署需确认迁移文件已随代码部署;若迁移历史不一致,可备份数据后重新生成迁移。python manage.py makemigrations python manage.py migrate - 检查数据库表:用数据库客户端连接部署环境数据库,确认
projectapp_customuser(对应你的应用名)表是否存在,不存在则说明迁移未执行成功。 - 重启应用服务:配置变更后重启应用服务器(如Gunicorn、uWSGI),确保新用户模型配置生效。
二、客户首页提示relation "projectapp_product"不存在
问题描述
访问store.html模板时出现错误:relation "projectapp_product" does not exist,相关代码如下:
store.html模板代码
{% extends 'customertemplates/main.html' %} {% load static %} {% block content %} <div class="row"> {% for product in products %} <div class="col-lg-4"> <img class="thumbnail" src="{{product.imageURL}}"> <div class="box-element product"> <h6><strong>{{product.product_name}}</strong></h6> <h4 style="display: inline-block;"><strong>${{product.discount_price}}</strong></h4> <hr> <button data-product="{{product.id}}" data-action="add" class="btn btn-outline-secondary add-btn update-cart"><i class='fa fa-shopping-cart yellow-color'></i> Add to Cart</button> {% if user.is_authenticated and user.is_customer %} <button style="width:110px" class="btn btn-outline-secondary add-btn add-wishlist" data-product="{{product.id}}" data-action="add"><i class="fa fa-heart"></i> Wishlist</button> {% endif %} <a class="btn btn-outline-success" href="{% url 'product_details' product.pk %}"><i class="fa fa-eye"></i> View</a> </div> </div> {% endfor %} </div> {% endblock content %}
store.views.py代码
def store(request): data = cartData(request) cartItems = data['cartItems'] order = data['order'] items = data['items'] products = Product.objects.all() context = {'products':products, 'cartItems':cartItems} return render(request, 'customertemplates/store.html', context)
Product模型代码
class Product(models.Model): id=models.AutoField(primary_key=True) url_slug=models.CharField(max_length=255) subcategories_id=models.ForeignKey(SubCategories,on_delete=models.CASCADE) product_name=models.CharField(max_length=255, null=True) brand=models.CharField(max_length=255) max_price=models.DecimalField(max_digits=7, decimal_places=2) discount_price=models.DecimalField(max_digits=7, decimal_places=2) product_description=models.TextField() product_long_description=models.TextField() in_stock_total=models.IntegerField(default=1) digital = models.BooleanField(default=False,null=True, blank=True) image = models.ImageField(null=True, blank=True) is_published = models.BooleanField(default=True) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return self.product_name @property def imageURL(self): try: url = self.image.url except: url = '' return url
排查与解决
- 执行模型迁移:确保Product模型的迁移已应用到部署环境数据库,运行以下命令:
检查迁移输出,确认python manage.py makemigrations projectapp # 替换为Product所在的应用名 python manage.py migrateprojectapp.Product的迁移已成功执行。 - 确认应用已注册:检查
settings.py的INSTALLED_APPS中是否包含Product模型所在的应用(如projectapp),未注册的应用模型不会生成数据库表。 - 检查数据库表:连接部署环境数据库,确认
projectapp_product表是否存在,不存在则排查迁移命令执行日志中的错误。 - 验证模型导入:检查视图文件中Product的导入语句是否正确(如
from projectapp.models import Product),避免因导入路径错误导致模型无法识别。
内容的提问来源于stack exchange,提问作者Shatila Shahi
相关产品推荐
相关产品推荐

