几个小时以来,我一直在努力看看这段代码有什么问题。我正在处理的项目要求用户注册到站点(由 Django 注册插件负责。注册后,用户将能够将其公司(姓名,地址,电话等)作为列表添加到站点。所以公司有自己的模式。我正在使用 Django 配置文件在 Django 配置文件页面上显示用户信息和公司信息。配置文件也建立在Django Profiles插件之上。
url(r'^accounts/', include('registration.urls')),
url(r'^admin_export/', include("admin_export.urls")),
url(r'^profiles/edit', 'profiles.views.edit_profile'),
url(r'^profiles/create', 'profiles.views.create_profile'),
url(r'^profiles/', include('profiles.urls')),
url(r'^profiles/(?P<username>w+)/$', 'profiles.views.profile_detail',name='UserProfileView'),
url(r'^comments/', include('django.contrib.comments.urls'))
#models.py
class UserProfile(models.Model):
user = models.ForeignKey(User,unique=True)
#email = models.CharField(max_length=200, blank=True, null=True)
# Other fields here
#company = models.ForeignKey(Company,blank=True,null=True)
#office = models.CharField(max_length=200, blank=True, null=True)
def __unicode__(self):
return self.user.username
class Company(models.Model):
userprofile = models.ForeignKey(UserProfile, null=True, blank=True)
comp_name = models.CharField(max_length=200,blank=True,null=True)
comp_address = models.CharField(max_length=200,blank=True, null=True)
comp_email = models.CharField(max_length=200,blank=True, null=True)
comp_zip = models.IntegerField(blank=True, null=True)
comp_phone = models.IntegerField(blank=True, null=True)
comp_city = models.CharField(max_length=200,blank=True, null=True)
#comp_state = models.USStateField(blank=True, null=True
comp_state = models.CharField(blank=True, max_length=2)
compwebsite = models.URLField(max_length=200, blank=True, null=True)
twitterurl = models.URLField(max_length=200, blank=True, null=True)
facebookurl = models.URLField(max_length=200, blank=True, null=True)
def __unicode__(self):
return self.comp_name
class ProfileForm(ModelForm):
class Meta:
model=UserProfile
exclude=('user',)
#views.py
def UserProfileView(request, username):
context_dict = {}
usercompany = get_object_or_404(Company, user=userprofile)
context_dict = {'usercompany': usercompany}
return render_to_response('profile_detail.html', context_dict, RequestContext(request))
userprofile
在您的views.py 中引用时实际上不可用,因此它应该引发NameError
如果我理解正确,实现这一目标的方法是:
#views.py
def UserProfileView(request):
context_dict = {}
usercompany = get_object_or_404(Company, userprofile__user=request.user)
context_dict = {'usercompany': usercompany}
return render_to_response('profile_detail.html', context_dict, RequestContext(request))