"WSGIRequest"对象没有属性"user" [英] 'WSGIRequest' object has no attribute 'user'

查看:58
本文介绍了"WSGIRequest"对象没有属性"user"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在django项目中创建一个auth模块.但是,当我打开网站URL时,出现以下错误:'WSGIRequest'对象没有属性'user'

I'am trying to make an auth module in my django project. But when I open my web site url I have a this error: 'WSGIRequest' object has no attribute 'user'

我正在尝试查找有关此问题的信息,有人说该问题在MIDDLEWARE_CLASSES中,但我听不懂.

I've trying to find information about this problem and somebody said that the problem is in MIDDLEWARE_CLASSES but I can't understand it.

这是我的MIDDLEWARE_CLASSES:

This is my MIDDLEWARE_CLASSES:

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    #'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

myproject/admin.py

myproject/admin.py

from django.contrib import admin
from personal_area.models import UserProfile

admin.site.register(UserProfile)

myproject/forms.py

myproject/forms.py

from personal_area.models import UserProfile
from django.contrib.auth.models import User
from django import forms


class UserForm(forms.Model):
    password = forms.CharField(widget=forms.PasswordInput())

    class Meta:
        model = User
        fields = ('username', 'email', 'password')


class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('website', 'picture')

myproject/models.py

myproject/models.py

from django.db import models
from django.contrib.auth.models import User


class UserProfile(models.Model):

    user = models.OneToOneField(User)

    website = models.URLField(blank=True)
    picture = models.ImageField(upload_to='profile_images', blank=True)

    def __unicode__(self):
        return self.user.username

myproject/views.py

myproject/views.py

from django.shortcuts import render, render_to_response, HttpResponseRedirect, RequestContext
from django.contrib import auth

from personal_area.forms import UserForm, UserProfileForm


def register(request):

    context = RequestContext(request)
    registered = False

    if request.method == 'Post':
        user_form = UserForm(data = request.POST)
        profile_form = UserProfileForm(data = request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()

            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit = False)
            profile.user = user

            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']

            profile.save()
            registered = True

        else:
            print(user_form.errors, profile_form.errors)

    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    return render_to_response('personal_area/register.html', {'user_form': user_form, 'profile_form': profile_form,
                                                              'registered': registered}, context)

myproject/register.html

myproject/register.html

{% extends "home/base.html" %}

 {% block auth %}
        <h1>Register with Rango</h1>

        {% if registered %}
        Rango says: <strong>thank you for registering!</strong>
        <a href="/personal_area/">Return to the homepage.</a><br />
        {% else %}
        Rango says: <strong>register here!</strong><br />

        <form id="user_form" method="post" action="/personal_area/register/"
                enctype="multipart/form-data">

            {% csrf_token %}

            {{ user_form.as_p }}
            {{ profile_form.as_p }}

            <input type="submit" name="submit" value="Register" />
        </form>
        {% endif %}
{% endblock %}

这是我的模块教程:

http://www.tangowithdjango.com/book/chapters/login.html#linking-together

推荐答案

您的中间件排序错误.请仔细阅读这部分.顺序应为:

Your middleware ordering is wrong. Please read this part carefully. The order should be:

'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',

这篇关于"WSGIRequest"对象没有属性"user"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆