在我网站的信息中心内的表单上呈现信息时出现问题,其中显示用户在注册时输入的信息。
我收到的错误消息是/ dashboard /上的AttributeError:' WSGIRequest'对象没有属性' profileuser'
对我做错了什么的想法?
Models.py:
from django.db import models
from django.contrib.auth.models import User
class ProfileUser(models.Model):
user = models.OneToOneField(User)
birthday = models.DateField(null=True,blank=True)
city = models.CharField(max_length=50,blank=True)
state = models.CharField(max_length=50,blank=True)
user_title = models.CharField(max_length=254, verbose_name="Influencer Level", blank=True)
user_points = models.IntegerField(null=True, verbose_name="Style Points", blank=True)
image = models.ImageField(upload_to='user_images/',null=True, blank=True)
#admin level and additional infomation
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
#Override the _unicode_() method to return out something meaningful
def __unicode__(self):
return ' %s %s %s %s %s %s' % (self.birthday, self.city, self.state, self.image, self.user_title, self.user_points)
views.py:
def dashboard(request):
template_var={}
if request.user.is_authenticated():
first_name=request.user.first_name
last_name=request.user.last_name
email=request.user.email
username=request.user.username
user = User.objects.get(first_name=first_name,last_name=last_name,email=email,username=username)
birthday=request.profileuser.birthday
city=request.profileuser.city
state=request.profileuser.state
title=request.profileuser.user_title
points=request.profileuser.user_points
image=request.profileuser.image
profileuser = ProfileUser.objects.filter(birthday=birthday, city=city, state=state, title=title,points=points,
image=image)
#Template
template_var["first_name"]=first_name
template_var["last_name"]=last_name
template_var["email"]=email
template_var["username"]=username
template_var["birthday"]=birthday
template_var["city"]=city
template_var["state"]=state
template_var["title"]=title
template_var["points"]=points
template_var["image"]=image
template_var["is_authenticated"]= True
return render_to_response("userprofile/dashboard.html",template_var, context_instance=RequestContext(request))
此处出现错误消息
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/dashboard/
Django Version: 1.6.2
Python Version: 2.7.1
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home',
'profiles',
'south')
Installed Middleware:
('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:
File "/Users/amechiegbe/Desktop/cm_dev/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/amechiegbe/Desktop/cm_dev/Indieitude/profiles/views.py" in dashboard
86. birthday=request.profileuser.birthday
Exception Type: AttributeError at /dashboard/
Exception Value: 'WSGIRequest' object has no attribute 'profileuser'
答案 0 :(得分:2)
您在数据库中查询用户并将其分配给变量user
。
然后你开始向请求中的profileuser
对象请求信息 - 但是,显然从错误中,请求中没有名为profileuser
的对象(并且通常不会有一个;标准Django会在当前登录用户的请求中放置一个名为“user”的对象,但它不会添加任何名为“profileuser”的内容。
您无需在数据库中搜索用户(您的user = User.objects.get(..)
)。您已经将用户设为request.user
。
您做需要从数据库中找到ProfileUser。这是一对一的相关模型吗?如果是这样,您可以使用您在个人资料models.py
中使用的属性名称从用户处获取该信息。这可能类似于profileuser = request.user.profile
。如果您没有将ProfileUser设置为一对一模型,则可以使用profileuser = ProfileUser.objects.get(username=request.user.username)
答案 1 :(得分:0)
您没有说明问题所在。您可以发布任何堆栈跟踪或更多有关问题的信息吗?
无论如何,在确定可能的原因时,将代码分成小部分并检查哪个部分负责该问题是一种好的做法。
在这种情况下,我会检查是否存在以下问题:
def dashboard(request):
template_var={}
if request.user.is_authenticated():
first_name=request.user.first_name
last_name=request.user.last_name
email=request.user.email
username=request.user.username
return render_to_response("userprofile/dashboard.html",template_var, context_instance=RequestContext(request))