我有一个Django的网页应用,一个产品应用和一个账户应用。当我在账户应用上注册一个新用户时,它成功地创建了一个用户,然后它被重定向到我的主页,那里显示了一堆产品(由超级用户控制)。但是当我使用新创建的用户而不是管理员登录http://127.0.0.1:8000/login/时,当登录成功时,我应该被重定向到我的主页,但出现错误:. RelatedObject Does not exist at /
用户没有客户,我还是新手,谢谢!
这是我的产品/models.py
from django.db import models
from django.contrib.auth.models import User
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField(max_length=100, null=True)
email = models.CharField(max_length=100, null=True)
My admin panel with all the models
The customer model
我的注册视图
from django.shortcuts import render, redirect
from .forms import RegisterForm
def register(response):
if response.method == "POST":
form = RegisterForm(response.POST)
if form.is_valid():
form.save()
return redirect('/')
else:
form = RegisterForm()
return render(response, "register/register.html", {'form':form})
产品应用程序views.py文件
def home(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
cartItems = order.get_cart_items
else:
items = []
order = {'get_cart_total':0, 'get_cart_items':0}
cartItems = order['get_cart_items']
#this gets all product objects in the database.
products = Product.objects.all()
#counts how many special offers we have by using count() function
special_offer = Product.objects.filter(special_offer=True).count()
#returns the average price of all the product objects that we have
aggr = Product.objects.aggregate(average_price=Avg('price'))
average_price = round(aggr['average_price'], 2)
#passes the product objects as a context to be passed to a template.
context = {'products' : products, 'special_offer' : special_offer, 'average_price' : average_price, 'cartItems' : cartItems}
return render(request, 'store.html', context)
回溯
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.0.6
Python Version: 3.8.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'TheApp',
'crispy_forms',
'register.apps.RegisterConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "\Python\Python38\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "\Python\Python38\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "\Python\Python38\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "\Desktop\django_projects\TheEcommerceProject\TheApp\views.py", line 13, in home
customer = request.user.customer
File "\Python\Python38\site-packages\django\utils\functional.py", line 225, in inner
return func(self._wrapped, *args)
File "\Python\Python38\site-packages\django\db\models\fields\related_descriptors.py", line 420, in __get__
raise self.RelatedObjectDoesNotExist(
Exception Type: RelatedObjectDoesNotExist at /
Exception Value: User has no customer.
3条答案
按热度按时间b91juud31#
我也学习了这一课。您必须是超级用户(admin),并将此admin添加到Customers
dy2hfwbg2#
首先,将related_name属性添加到客户模型中的onetoonefield,与下面的代码相同
进行迁移并迁移,然后重试
lnlaulya3#
Prob你忘了在www.example.com文件中注册你的客户类admin.py至于我,我得到了这个问题后,我删除了客户的数据库。错误弹出时,我登录到我的管理员帐户,并试图查看网站。我刚刚在管理面板中添加了客户的个人资料。