gpt4 book ai didi

python - 登录 Django 时帐户未进行身份验证

转载 作者:行者123 更新时间:2023-11-30 00:03:45 26 4
gpt4 key购买 nike

我可以注册帐户并将数据上传到MYSQL数据库,但帐户没有“身份验证”

views.py

from django.core.urlresolvers import reverse_lazy
from django.views.generic import FormView, TemplateView
from django.shortcuts import redirect, get_object_or_404, render, render_to_response
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from django.contrib import messages, auth
from django.http import HttpResponseRedirect, HttpResponse
from django.core.context_processors import csrf
from mongoengine.queryset import DoesNotExist

from .forms import *

class RegisterView(FormView):
template_name = 'registration/register.html'
form_class = UserCreationForm
success_url = reverse_lazy('registered')

def form_valid(self, form):
form.save()
return FormView.form_valid(self, form)

register = RegisterView.as_view()


class RegisteredView(TemplateView):
template_name = 'registration/registered.html'

registered = RegisteredView.as_view()

def login(request):
c = {}
c.update(csrf(request))
return render_to_response('registration/auth.html', c)

def auth_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
else:
return HttpResponseRedirect('/inactive')
else:
return HttpResponseRedirect('/invalid')

def loggedin(request):
return render_to_response('registration/hello.html',
{'full_name': request.user.username})

def invalid_login(request):
return render_to_response('registration/hello.html')

Auth_view() 负责处理登录。

基本上,无论我做什么,它总是会遇到带有未知错误标志的“异常(exception):”。我觉得它甚至没有连接 MYSQL 数据库并“验证”

编辑:更新了 Views.py/auth_view()

用户创建表单 (forms.py)

   class UserCreationForm(forms.Form):
error_messages = {
'duplicate_username': _("A user with that username already exists."),
'password_mismatch': _("The two password fields didn't match."),
}
username = forms.RegexField(label=_("Username"), max_length=30,
regex=r'^[\w.@+-]+$',
help_text=_("Required. 30 characters or fewer. Letters, digits and "
"@/./+/-/_ only."),
error_messages={
'invalid': _("This value may contain only letters, numbers and "
"@/./+/-/_ characters.")})
email = forms.EmailField(label=_("Email"), max_length=254,
help_text=_("Required valid email. 254 characters or fewer."),
error_messages={
'invalid': _("This value may contain only valid email address.")})
password1 = forms.CharField(label=_("Password"),
widget=forms.PasswordInput)
password2 = forms.CharField(label=_("Password confirmation"),
widget=forms.PasswordInput,
help_text=_("Enter the same password as above, for verification."))

def clean_username(self):
username = self.cleaned_data["username"]
try:
User._default_manager.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError(
self.error_messages['duplicate_username'],
code='duplicate_username',
)

def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError(
self.error_messages['password_mismatch'],
code='password_mismatch',
)
return password2

def save(self):
user = User._default_manager.create_user(
username=self.cleaned_data['username'],
email=self.cleaned_data['email'],
password=self.cleaned_data["password1"])
return user

编辑2:

最佳答案

嗯,我认为你必须使用 django 的身份验证:

user = authenticate(username=username, password=password)

有关如何使用 django 身份验证的更多信息,请参阅此页面:

https://docs.djangoproject.com/en/dev/topics/auth/default/#auth-web-requests

顺便说一句,如果你可以注册用户,这意味着你的 django 应用程序与数据库连接良好,问题一定出在其他地方。

关于python - 登录 Django 时帐户未进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24783922/

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