Django ';匿名用户';对象没有属性';配置文件';-匿名和经过身份验证的用户访问视图

Django ';匿名用户';对象没有属性';配置文件';-匿名和经过身份验证的用户访问视图,django,django-templates,django-views,httpresponse,Django,Django Templates,Django Views,Httpresponse,我有以下用户模式: class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField(unique=True, null=True) username = models.CharField(_('username'), max_length=30, null=True) first_name = models.CharField(_('first name'), max_length=30

我有以下用户模式:

class User(AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(unique=True, null=True)
    username = models.CharField(_('username'), max_length=30, null=True)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)

    objects = UserManager()
    USERNAME_FIELD = "email"

    def __str__(self):
        return "@{}".format(self.email)
我有
UserProfile

class UserProfile(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='profile'
    )
根据以前的模型,一个用户有一个userprofile。好的

我有一些观点认为,有意思的是,匿名用户和登录用户都可以访问它们

当登录用户访问视图时,将询问配置文件数据信息并在模板中显示它们

当匿名用户访问视图时,这应该呈现模板,显然没有配置文件数据信息

该视图名为
article\u detail
,其小逻辑如下:

def article_detail(request, slug):
    user = request.user

    # I ask fot the profile user 
    profile = user.profile

    queryset = Article.objects.filter(published_date__lte=timezone.now())
    article = get_object_or_404(Article, slug=slug)
    return render(request, 'blog/article_detail.html',
                    {'article': article,'userprofile':profile })
当经过身份验证的用户访问此视图时,将呈现数据配置文件信息,验证
profile=user.profile
部分代码。这没问题

但当匿名用户访问此视图时,我收到以下消息:

'AnonymousUser' object has no attribute 'profile'  
当用户执行请求时,我尝试使用
is\u authenticated()
函数:

def article_detail(request, slug):
    user = request.user
    if user.is_authenticated():
        profile = user.profile
        queryset = Article.objects.filter(published_date__lte=timezone.now())
        article = get_object_or_404(Article, slug=slug)

        context = {'article': article, 'userprofile':profile }
        return render(request, context,
                        'blog/article_detail.html')
但当我在用户未经身份验证的情况下访问视图时,我会收到以下消息:

The view blog.views.article_detail didn't return an HttpResponse object. It returned None instead
raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: {'comments': <QuerySet []>, 'article': <Article: Los gatos callejeros>, 'userprofile': <UserProfile: @botibagl@gmail.com>}
[12/Aug/2017 00:01:07] "GET /article/los-gatos-callejeros/ HTTP/1.1" 500 105628
当我在用户通过身份验证时访问视图时,我收到以下消息:

The view blog.views.article_detail didn't return an HttpResponse object. It returned None instead
raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: {'comments': <QuerySet []>, 'article': <Article: Los gatos callejeros>, 'userprofile': <UserProfile: @botibagl@gmail.com>}
[12/Aug/2017 00:01:07] "GET /article/los-gatos-callejeros/ HTTP/1.1" 500 105628
raisetemplatedoesnotexist(模板名称,chain=chain)
django.template.exceptions.TemplateDoesNotExist:{'comments':,'article':,'userprofile':}
[2017年8月12日00:01:07]“GET/article/los gatos callejeros/HTTP/1.1”500 105628

我的问题可能与身份有关吗?

您的视图不适用于经过身份验证的用户,因为在这一行中:

        return render(request, context,
                        'blog/article_detail.html')
您使用
上下文
切换了模板名称-它应该是:

        return render(request, 'blog/article_detail.html', context)
为了让视图与匿名用户一起工作,它必须返回HttpResponse,您的版本返回
None
。您可以这样重写您的视图:

def article_detail(request, slug):
    user = request.user
    article = get_object_or_404(Article, slug=slug)
    context = {'article': article}
    if user.is_authenticated():
        context['profile'] = user.profile
    return render(request, 'blog/article_detail.html', context)

您的视图不适用于经过身份验证的用户,因为在此行中:

        return render(request, context,
                        'blog/article_detail.html')
您使用
上下文
切换了模板名称-它应该是:

        return render(request, 'blog/article_detail.html', context)
为了让视图与匿名用户一起工作,它必须返回HttpResponse,您的版本返回
None
。您可以这样重写您的视图:

def article_detail(request, slug):
    user = request.user
    article = get_object_or_404(Article, slug=slug)
    context = {'article': article}
    if user.is_authenticated():
        context['profile'] = user.profile
    return render(request, 'blog/article_detail.html', context)

您将用户作为匿名用户的唯一原因是您未登录或用户未经身份验证,因此请在打开应用程序时登录您的帐户。问题已得到解决。

您将用户作为匿名用户的唯一原因是您未登录或用户未经身份验证,所以,请在你打开应用程序的那一刻登录到你的帐户,问题就解决了