gpt4 book ai didi

python - Django 'AnonymousUser' 对象没有属性 '_meta'

转载 作者:太空狗 更新时间:2023-10-29 17:42:39 26 4
gpt4 key购买 nike

我在我的 Django 应用程序中使用社交登录。因此,我在我的 settings.py 文件中添加了额外的后端。

AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'social_core.backends.open_id.OpenIdAuth',
'social_core.backends.google.GoogleOpenId',
'social_core.backends.google.GoogleOAuth2',
'social_core.backends.google.GoogleOAuth',
'social_core.backends.twitter.TwitterOAuth',
'social_core.backends.facebook.FacebookOAuth2',
'social_core.backends.github.GithubOAuth2',

]

我还使用 UserCreationForm 进行注册,

class SignupForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required=True, help_text='Required.')
last_name = forms.CharField(max_length=30, required=True, help_text='Required.')
email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')

class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2' )

这是 View 文件,

def signup(request):
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_pass = form.cleaned_data.get('password')
user = authenticate(request, username=username, password=raw_pass)
login(request,user,backend='django.contrib.auth.backends.ModelBackend')
url = reverse('location:get_location')
print("location_url ", url)
return HttpResponseRedirect(url)
else:
form = SignupForm()
return render(request, 'signup.html', {'form':form})

现在,当我点击表单上的注册按钮时出现此错误,

'AnonymousUser' object has no attribute '_meta'

在线,

login(request,user,backend='django.contrib.auth.backends.ModelBackend')

为什么会这样?

我可以在管理面板中看到用户已保存。

是什么导致了这个错误?以及如何解决?

编辑 -

Internal Server Error: /signup/
Traceback (most recent call last):
File "/home/luvpreet/Envs/weather/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/luvpreet/Envs/weather/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/luvpreet/Envs/weather/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/luvpreet/Desktop/drf-vogo/weather/weather/pilot/views.py", line 45, in signup
login(request,user,backend='django.contrib.auth.backends.ModelBackend')
File "/home/luvpreet/Envs/weather/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 154, in login
request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
File "/home/luvpreet/Envs/weather/local/lib/python2.7/site-packages/django/utils/functional.py", line 239, in inner
return func(self._wrapped, *args)
AttributeError: 'AnonymousUser' object has no attribute '_meta'

最佳答案

当你保存表单时你已经有了用户,所以你不需要调用 authenticate 因为你已经在调用 login() 时提供了后端:

user = form.save()
login(request, user, backend='django.contrib.auth.backends.ModelBackend')

关于python - Django 'AnonymousUser' 对象没有属性 '_meta',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46284664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com