django customuser匹配查询不存在

时间:2013-03-03 19:56:51

标签: django django-models django-forms django-views

我认为它起源于这种观点,因为它被转移到了家庭观点(见下文):

@login_required
def HomePage(request):
    if request.user.get_profile().phone == "":
        return HttpResponseRedirect('/phone')
    else:
        add_list = add.objects.all()
        ctx = {'last_login':request.session.get('social_auth_login_backend')}
        return render_to_response("homepage.html", {'add_list': add_list, 'ctx':ctx}, context_instance=RequestContext(request))

模特:

class customuser(models.Model):
    last_login = models.DateTimeField(blank=True, null=True)
    is_active = models.BooleanField()
    nickname = models.CharField(max_length=255)
    regdate = models.DateField(auto_now=True)
    phone = models.CharField(max_length=255)
    user = models.OneToOneField(User)
    city = models.ForeignKey(locations, null=True)

    def __str__(self):
        return "%s user " % self.user

    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            profile, created = customuser.objects.get_or_create(user=instance)

    post_save.connect(create_user_profile, sender=User)

    class Admin:
        pass    

1 个答案:

答案 0 :(得分:0)

我不确定这是否是您遇到问题的原因,但是您传递给模板的上下文字典看起来不对。应该重写该视图 -

def HomePage(request):
    if request.user.get_profile().phone == "":
        return HttpResponseRedirect('/phone')
    else:
        ctx = {'last_login':request.session.get('social_auth_login_backend'),
               'add_list': add.objects.all()}
        return render(request, "homepage.html", ctx)