【问题标题】:Overriding Django profiles' profile_detail view覆盖 Django 配置文件的 profile_detail 视图
【发布时间】:2012-09-30 11:23:30
【问题描述】:

我安装了 django 配置文件/注册,一切似乎都很好。当用户注册时,他们的个人资料也会被创建。现在我要做的是根据用户的用户 ID 查询另一个公司模型。我不想更改 django-profiles 视图,而是在 url 上添加额外字段以匹配和查询公司模型。当我对 url 进行硬编码时(例如:输入 userprofile 的 id 号,就像 userprofile=1 一样,它可以工作。)。因此,当用户登录并转到个人资料详细信息页面时,分配给他们的公司会根据他们的 user.id 进行查询。

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    #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



url(r'^profiles/(?P<username>\w+)/$', 'profiles.views.profile_detail', {'extra_context':{'queryset':Company.objects.filter(userprofile=request.user.id)}},),

【问题讨论】:

标签: django django-profiles


【解决方案1】:

您可能想从视图内部调用它

from *** import profile_detail

def my_view(request, username):
    extra_context = {}
    return profile_detail(request, queryset=Company.objects.filter(userprofile=request.user.id),
                       template_name="my_template.html",
                       paginate_by=20,
                       extra_context=extra_context)

【讨论】:

  • 我没有在视图中调用它的原因是因为它是第三方应用程序 => 'Django 配置文件"。结果我的问题很容易通过调用 user.userprofile.company_set.all 来解决.
  • 你可以在 urlconf 本身中做到这一点吗?
  • No not in urls, in template.
  • 啊,是的,这也有效。不太确定它与您的问题有关。
猜你喜欢
  • 2013-07-14
  • 2019-07-02
  • 2018-11-29
  • 2012-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多