2013-03-08 46 views
0

我試圖做一個應用程序,寵物的所有者可以創建自己的配置文件並上傳他們的寵物的圖片。Django錯誤的本地變量'人'參考前

當用戶創建一個帳戶或登錄時,他會被重定向到配置文件中。配置文件頁面應顯示他的信息,例如名稱,而不是我收到此錯誤。

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)) 

我profile.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 
+1

'人 - request.user.get_profile'應該是'人= request.user.get_profile()' – 2013-03-08 10:13:31

+0

是啊,我仍然得到錯誤 – donkeyboy72 2013-03-08 10:15:29

+0

它; s代碼的排列 – catherine 2013-03-08 10:16:19

回答

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)) 
+0

你的位置!謝謝:] – donkeyboy72 2013-03-08 10:25:49

+0

,在賦值之前引用局部變量'Person'是什麼意思? – donkeyboy72 2013-03-08 10:27:30

+0

您呈現空的Person變量。如果你渲染一個變量,你必須檢查它是否正確調用並定義它。 – catherine 2013-03-08 10:36:59