【问题标题】:Django user profile queryDjango 用户配置文件查询
【发布时间】:2012-10-08 16:32:07
【问题描述】:

几个小时以来,我一直在努力查看这段代码有什么问题。我正在进行的项目呼吁用户注册到网站(由Django registration plugin 负责。)一旦注册,用户将能够将他们的公司(姓名、地址、电话等)添加到网站作为清单。所以公司有自己的模式。我正在使用 Django 个人资料在 Django 个人资料页面上显示用户信息和公司信息。配置文件也建立在Django Profiles plugin 之上。

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

【问题讨论】:

  • 如果有更多关于什么不起作用的信息,这可能会更容易。你有错误吗?
  • 没有错误,但我无法在个人资料页面上呈现公司信息。请参阅底部的views.py。我的目标是呈现当前登录用户提交的公司信息。

标签: django django-profiles


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2012-11-14
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    相关资源
    最近更新 更多