我无法解决这个问题,每次显示views.register_user
都没有返回Httppresponse.用户注册也不正常,每次都会显示上面的错误文本.昨天它工作正常,但今天显示错误.那么,如何修复这个错误,如何解决这个问题呢?我已经张贴了我的详细代码,请查收.此外,我已经申请了堆栈溢出答案,但没有工作.
下面是我的代码:
Views.py
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.forms import UserCreationForm
from django.contrib import messages
from .forms import SignUpForm
def register_user(request):
if request.method == 'POST':
form=SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data['username']
password = form.cleaned_data['password1']
user = authenticate(request, username=username, password = password)
login(request,user)
messages.success(request,('You Have Been Registered!'))
return redirect('home')
else:
form = SignUpForm()
context = {'form': form}
return render(request,'authenticate/Register.html',context)
Register.html
{% extends 'authenticate/base.html' %}
{% block content %}
<h2 class="text-center">Register</h2>
<div class="col-md-6 offset-md-3">
<form method="POST" action ="{% url 'register' %}">
{% csrf_token %}
{% if form.errors %}
<p>Your Form Has Errors.....</p>
{% endif %}
{{ form.as_p }}
<input type="submit" value="Register" class="btn btn-secondary">
</form>
</div>
<br/><br/>
{% endblock %}
Forms.py
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
class SignUpForm(UserCreationForm):
email = forms.EmailField(label="",widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Email Address'}))
first_name = forms.CharField(label="",max_length=100, widget=forms.TextInput(attrs={'class':'form-control','placeholder':'First Name'}))
last_name = forms.CharField(label="",max_length=100,widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Last Name'}))
class Meta:
model = User
fields = ('username','first_name','last_name','email','password1','password2')
def __init__(self, *args, **kwargs):
super(SignUpForm, self).__init__(*args, **kwargs)
self.fields['username'].widget.attrs['class'] = 'form-control'
self.fields['username'].widget.attrs['placeholder'] = 'User Name'
self.fields['username'].label=""
self.fields['username'].help_text='<span class="form-text text-muted"><small>Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.</small></span>'
self.fields['password1'].widget.attrs['class'] = 'form-control'
self.fields['password1'].widget.attrs['placeholder'] = 'Password'
self.fields['password1'].label=""
self.fields['password1'].help_text='<ul class="form-text text-muted"><li>Your password can’t be too similar to your other personal information.</li><li>Your password must contain at least 8 characters.</li><li>Your password can’t be a commonly used password.</li><li>Your password can’t be entirely numeric.</li></ul>'
self.fields['password2'].widget.attrs['class'] = 'form-control'
self.fields['password2'].widget.attrs['placeholder'] = 'Confirm Password'
self.fields['password2'].label=""
self.fields['password2'].help_text='<span class="form-text text-muted"><small>Enter the same password as before, for verification.</small></span>'