0

我正在尝试制作一个应用程序,宠物主人可以在其中创建自己的个人资料并上传宠物的照片。

当用户创建帐户或登录时,他会被重定向到配置文件。个人资料页面应显示他的信息,例如姓名,而不是我收到此错误。

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 链接到我的个人资料功能

我的观点.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))

我的个人资料.html

{% if Person %}
<ul>
        <li>Name : {{Person.user}} </li>
</ul>
{% endif %}     

我的模型.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
4

1 回答 1

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))
于 2013-03-08T10:17:54.903 回答