我正试图在Django中编写一个注册表单,但在发布时,我一直收到一个Validation错误
'date_joined':[ValidationError([此字段为必填字段。'](
表单基于django用户模型。我在表格上没有这个字段,因为它的值可以在代码中设置
html
<form method="post" action="{% url 'sign-up' %}">
{% csrf_token %}
<h1>Bid for Game - Sign Up</h1>
Your first and last names will be used to help your friends identify you on the site.
<table>
<tr>
<td>{{ signup_form.first_name.label_tag }}</td>
<td>{{ signup_form.first_name }}</td>
</tr>
<tr>
<td>{{ signup_form.last_name.label_tag }}</td>
<td>{{ signup_form.last_name }}</td>
</tr>
<tr>
<td>{{ signup_form.username.label_tag }}</td>
<td>{{ signup_form.username }}</td>
</tr>
<tr>
<td>{{ signup_form.email.label_tag }}</td>
<td class='email'>{{ signup_form.email }}</td>
</tr>
<tr>
<td>{{ signup_form.password.label_tag }}</td>
<td><class='password'>{{ signup_form.password }}</class></td>
</tr>
<tr>
<td>Confirm password: </td>
<td class='password'>{{ signup_form.confirm_password }}</class</td>
</tr>
</table>
<button type="submit" class="btn btn-success">Submit</button>
</form>
表单.py
class SignupForm(forms.ModelForm):
first_name = forms.CharField(label='First name', max_length=30)
last_name = forms.CharField(label='Last name', max_length=30)
username = forms.CharField(label='User name', max_length=30)
email = forms.EmailField(label='Email address', widget=forms.EmailInput(attrs={'class': 'email'}))
password = forms.PasswordInput()
confirm_password=forms.CharField(widget=forms.PasswordInput())
date_joined = forms.DateField()
class Meta:
model = User
fields = '__all__'
widgets = {
'password': forms.PasswordInput(),
'password_repeat': forms.PasswordInput(),
}
@staticmethod
def initialise(request):
"""Return a dict of initial values."""
initial = {
'first_name': request.POST['first_name'],
'last_name': request.POST['last_name'],
'username': request.POST['username'],
'email': request.POST['email'],
'date_joined': datetime.datetime.today()
}
return initial
视图.py
class SignUp(View):
url = "users/signup.html"
form_context = {
'signup_form': SignupForm,
}
def get(self, request):
context = self.form_context
return render(request, self.url, context)
def post(self, request):
signup_form = SignupForm()
signup_form.initial = SignupForm.initialise(request)
context = {'signup_form': signup_form}
form = SignupForm(request.POST)
if form.is_valid():
print('valid')
else:
print(form.errors.as_data())
print('invalid')
由于date_joind是必填字段,我该在哪里设置它?
所以很多时候,我看到人们使用东西,然后免费覆盖它为他们做的一切。上面的形式和视图可以简化为类似的东西(未经测试,但这是一个足够好的开始(:
class SignupForm(forms.ModelForm):
password = forms.CharField(widget=PasswordInput)
confirm_password = forms.CharField(widget=PasswordInput)
class Meta:
model = User
exclude = ("date_joined",)
from django.views.generic import CreateView
class SignupView(CreateView):
form_class = SignupForm
def form_valid(self, form):
data = form.cleaned_data
password = data.pop('password')
confirm_password = data.pop('confirm_password')
if password != confirm_password:
form.add_error('Passwords do not match')
return self.form_invalid(form)
user = User.objects.create_user(**data)
user.set_password(password)
user.save()