我一直在尝试创建注册用户的登录信息并将其验证到网站中。由于某种原因,它在执行时,form.is_valid()总是返回false,即使我将其指向正确的表单。以下是我的views.py文件
from django.views import generic
from django.views.generic.base import TemplateView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import AuthenticationForm
from django.views.generic import View
from django.urls import reverse_lazy
from django.http import HttpResponse
from .registrationform import UserForm
from .loginform import UserLoginForm
from django.contrib import messages
from django.contrib.auth.models import User
class IndexView(View):
form_class = UserLoginForm
template_name = 'login/login_form.html'
def get(self, request):
form = self.form_class(None)
return render(request, self.template_name, {'form': form})
def post(self, request):
form = self.form_class(request.POST)
print(form.is_valid())
if form.is_valid():
user = form.save(commit=False)
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None and user.is_active():
login(request, user)
return redirect('sports:index')
else:
return redirect('login:login')
else:
return HttpResponse(request, status=500)
class CreateUser(View):
form_class = UserForm
template_name = 'login/registration_form.html'
def get(self, request):
form = self.form_class(None)
return render(request, self.template_name, {'form': form})
def post(self, request):
form = self.form_class(request.POST)
print(form.is_valid())
if form.is_valid():
user = form.save(commit=False)
email = form.cleaned_data['email']
username = form.cleaned_data['username']
pass1 = form.cleaned_data['password']
pass2 = form.cleaned_data['repeatpassword']
if pass1 != pass2:
messages.add_message(request, messages.ERROR, 'Passwords do not match')
return redirect('login:register')
else:
user = User.objects.create_user(username=username, email=email, password=pass1)
user.save()
return redirect('login:login')
return HttpResponse(request, status=200)
else:
return HttpResponse(request, status=500)
这是我的urls.py页面
from django.contrib import admin
from django.urls import path, include
from . import views
from django.contrib.auth.views import login
app_name = 'login'
urlpatterns = [
path('', views.IndexView.as_view(), name="login"),
path('register', views.CreateUser.as_view(), name='register')
]
这是我的login_forms.html页面
<form class="" action="{% url 'login:login' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% csrf_token %}
{% for field in form %}
<label>{{ field.label_tag }}</label>
<div>{{ field }}</div>
{% endfor %}
<button type="submit" name="register">Register</button>
创建帐户{%endblock%}
这是我的registration_form.html
{% extends 'sports/base.html' %}
{% block body %}
<title>{% block title %} Register {% endblock %}</title>
<h1>Create Account</h1>
<form class="" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% for field in form %}
<label>{{ field.label_tag }}</label>
<div>{{ field }}</div>
{% endfor %}
<button type="submit" name="register">Register</button>
</form>
{% if messages %}
{% for message in messages %}
<p>{{ message }}</p>
{% endfor %}
{% endif %}
{% endblock %}
我不明白的是,我几乎复制了用于注册用户登录用户的代码,但即使我将其指向正确的template_name,它似乎也返回false。
IndexView类应该登录用户并链接到login_form.html,CreateUser类用于注册用户并链接到registration_form.html。
我是学习Django的新手,很高兴能得到任何帮助。