我试图创建一个应用程序,让宠物主人可以创建自己的个人资料并上传他们宠物的照片。
当用户创建帐户或登录时,他会被重定向到个人资料中。个人资料页面应该显示他的信息,例如姓名,而不是我收到此错误。
UnboundLocalError at /profile/
local variable 'Person' referenced before assignment
Request Method: GET
Request URL: /profile/
Django Version: 1.4.3
Exception Type: UnboundLocalError
Exception Value: local variable 'Person' referenced before assignment
Traceback Switch to copy-and-paste view
C:\o\mysite\pet\views.py in Profile
60. return render_to_response('profile.html', {'Person': Person}, context_instance=RequestContext(request))
问题是在views.py
链接到我的个人资料功能我的views.py的部分
@login_required
def Profile(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('/login/')
Person = request.user.get_profile
return render_to_response('profile.html', {'Person': Person}, context_instance=RequestContext(request))
我的个人资料。
{% if Person %}
<ul>
<li>Name : {{Person.user}} </li>
</ul>
{% endif %}
我的models.py
from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.models import User
class Person(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=100)
image = models.FileField(upload_to="images/",blank=True,null=True)
def __unicode__(self):
return self.name
class Pet(models.Model):
Person = models.ForeignKey(Person)
description = models.CharField(max_length=100)
image = models.FileField(upload_to="images/",blank=True,null=True)
def __unicode__(self):
return self.description
答案 0 :(得分:1)
尝试将Person放在request.user.is_authenticated的顶部。你忘了把'()'放在get_profile
中@login_required
def Profile(request):
Person = request.user.get_profile()
if not request.user.is_authenticated():
return HttpResponseRedirect('/login/')
return render_to_response('profile.html', {
'Person': Person
}, context_instance=RequestContext(request))