【发布时间】:2019-01-28 09:14:14
【问题描述】:
我在 django 中使用默认的path('',include("django.contrib.auth.urls")) 为我的项目执行登录、密码重置操作,我已经彻底检查了我的注册表单和数据库,注册部分一切正常,但我无法验证所有除了超级用户之外的其他用户,这个问题可能是什么原因?
myproject/urls.py:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', include('home.urls')),
path('accounts/',include('accounts.urls')),
path('',include("django.contrib.auth.urls"))
]
在注册目录的模板中,我的登录表单看起来像
{% extends 'base.html' %}
{% block title %}Login{% endblock %}
{% block content %}
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
{% endblock %}
我的注册视图是:
class UserFormView(View):
form_class = RegForm
template_name = 'signup.html'
def get(self, request):
form = self.form_class()
return render(request, self.template_name, {'form': form})
def post(self, request):
form = self.form_class(request.POST)
if (form.is_valid()):
form.save()
return redirect('login')
return render(request, self.template_name, {'form': form})
然后是我的表格:
class RegForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
confirm_password=forms.CharField(widget=forms.PasswordInput())
class Meta:
model= User
fields=['first_name','last_name','username','email','date_joined','password','confirm_password']
def clean_password(self):
password=self.cleaned_data.get("password")
confirm_password = self.cleaned_data.get("confirm_password")
if(len(password)<8):
raise forms.ValidationError("The length of the password should be minimum 8 characters")
return password
def clean_email(self):
email=self.cleaned_data.get('email')
if(validate_email(email)==False):
raise forms.ValidationError("The Email Format is In Correct")
return email
def clean_confirm_password(self):
password = self.cleaned_data.get("password")
confirm_password = self.cleaned_data.get("confirm_password")
if (password != confirm_password):
raise forms.ValidationError('Password doesn\'t match')
【问题讨论】:
-
其他用户检查
is_active是否设置为True? -
是的,我已经设置好了,但仍然有同样的问题@ShafikurRahman
-
它是否显示错误?需要更多细节
-
你需要展示一些代码。你是如何创建用户的?那你怎么认证?显示注册和登录表单和视图。
-
一直显示错误,"请输入正确的用户名和密码。注意这两个字段可能区分大小写。",
标签: django django-models django-forms django-views