' AnonymousUser'对象没有属性' _meta'

时间:2018-05-14 04:43:35

标签: python django

views.py代码

from django.shortcuts import render,redirect
from django.http import HttpResponse
from django.contrib.auth.forms import UserCreationForm
from tutorial import views
from accounts.form import Registrationform,EditProfileForm
from django.contrib.auth.forms import UserChangeForm,PasswordChangeForm
from django.contrib.auth.models import User
from django.contrib.auth import update_session_auth_hash
# Create your views here.

def home(request):
    numbers=[1,2,3,4,5]
    name="jay"
    args={'myName':name}

    return render(request,'accounts/home.html',args)
def register(request):
    if request.method == "POST":
        form = Registrationform(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/account')
        else:
            return HttpResponse('please fill all the fields and make sure new password must be match')
    else:
        form = Registrationform()
        args={'form' : form}
        return render(request,'accounts/reg_form.html',args)

def view_profile(request):
    args = {'user' : request.user}
    return render(request,'accounts/profile.html',args)

def edit_profile(request):
    if request.method=="POST":
        form = EditProfileForm(request.POST,instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('/account/profile')
    else:
        form  =  EditProfileForm(instance=request.user)
        args = {'form' : form}
        return render(request,'accounts/edit_profile.html',args)

def change_password(request):
    if request.method == "POST":
        form = PasswordChangeForm(data=request.POST,user=request.user)
        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            return redirect('/account/profile')
        else:
            return HttpResponse("password doest not match,go back and try again")
    else:
        form = PasswordChangeForm(user=request.user)
        args = {'form' : form}
        return render(request,'accounts/change_password.html',args)

和form.py是

from django import forms
from django.contrib.auth.forms import UserCreationForm,UserChangeForm
from django.contrib.auth.models import User
#from django.forms import RegistrationForm,EditProfileForm

class Registrationform(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = (
        'username',
        'first_name',
        'last_name',
        'email',
        'password1',
        'password2',
        )
    def save(self,commit=True):
        user=super(Registrationform,self).save(commit=False)
        user.first_name= self.cleaned_data['first_name']
        user.last_name=self.cleaned_data['last_name']
        user.email=self.cleaned_data['email']
        if commit:
            user.save()
        return user
class EditProfileForm(UserChangeForm):
    #template_name = '/something/else'
    class Meta:
        model = User
        fields = (
        'email',
        'first_name',
        'last_name',
        'password'
         )

和urls.py

#python -m smtpd -n -c DebuggingServer localhost:1025
from django.contrib import admin
from .import views
from django.conf.urls import url,include
from django.contrib.auth.views import login,logout,password_reset,password_reset_done,password_reset_confirm,password_reset_complete
#from django.urls import path

urlpatterns = [
    url(r'^$',views.home),
    url(r'^login/$',login,{'template_name' : 'accounts/login.html'}),
    url(r'^logout/$',logout,{'template_name' : 'accounts/logout.html'}),
    url(r'^register/$',views.register,name='register'),
    url(r'^profile/$',views.view_profile,name='view_profile'),
    url(r'^profile/edit/$',views.edit_profile,name='edit_profile'),
    url(r'^change-password/$',views.change_password,name='change_password'),
    url(r'^reset-password/$',password_reset,name='reset_password'),
    url(r'^reset-password/done/$',password_reset_done,name='password_reset_done'),
    url(r'^reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
    password_reset_confirm,name='password_reset_confirm' ),
    url(r'^reset-password-complete/$',password_reset_complete,name='password_reset_complete'),


]

所以,当我输入网址 - &#34; http://127.0.0.1:8000/account/profile/edit/&#34;它给我错误

  

&#34;&#39; AnonymousUser&#39;对象没有属性&#39; _meta&#39;&#34;

错误是什么?我在这里做错了什么?和django给出这一行错误

  

/usr/local/lib/python2.7/dist-packages/django/utils/functional.py in   内部,第205行。

我该怎么做才能解决这个错误?

2 个答案:

答案 0 :(得分:4)

使用login_required装饰器会更好。它会在没有明确编码的情况下检查用户是否登录。

from django.contrib.auth.decorators import login_required

@login_required
def edit_profile(request):

     if request.method=="POST":
        form = EditProfileForm(request.POST,instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('/account/profile')
     else:
        form  =  EditProfileForm(instance=request.user)
     args = {'form' : form}
     return render(request,'accounts/edit_profile.html',args)

答案 1 :(得分:3)

您尚未登录并尝试修改个人资料。要防止出现此类错误,请在视图中添加is_authenticated验证。如果用户未经过身份验证,您可以使用redirect快捷方式将其重定向到login页面:

def edit_profile(request):
  if request.user.is_authenticated:  
     if request.method=="POST":
        form = EditProfileForm(request.POST,instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('/account/profile')
     else:
        form  =  EditProfileForm(instance=request.user)
        args = {'form' : form}
        return render(request,'accounts/edit_profile.html',args) 
  else:
     return redirect('/login/')