我设置了表单并且工作没有错误,但是在添加了带有 RadioSelect 小部件的 IntegerFIeld 后,表单不再验证。(在此之前,我在模型中只有 CharFields,没有小部件)
我搜索了其他类似的问题,但找不到任何解决此问题的方法。
目前我只是不断收到我在views.py中编码的错误消息。
我的设置如下:
视图.py
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.utils import timezone
from .forms import DiaryEntryForm
from .models import DiaryEntry
def new_entry(request):
if request.method == "POST":
form = DiaryEntryForm(request.POST)
if form.is_valid():
entry = form.save(commit=False)
entry.author = request.user
entry.created_date = timezone.now()
entry.save()
messages.success(request, "Entry created successfully")
return redirect(entry_detail, entry.pk)
else:
messages.error(request, "There was an error saving your entry")
return render(request, 'diaryentryform.html', {'form': form})
else:
form = DiaryEntryForm()
return render(request, 'diaryentryform.html', {'form': form})
表格.py
from django import forms
from .models import DiaryEntry, FORM_CHOICES
class DiaryEntryForm(forms.ModelForm):
body = forms.ChoiceField(widget=forms.RadioSelect(),choices=FORM_CHOICES)
mind = forms.ChoiceField(widget=forms.RadioSelect(),choices=FORM_CHOICES)
class Meta:
model = DiaryEntry
fields = ('body', 'mind', 'insights')
模型.py
from __future__ import unicode_literals
from django.db import models
from django.utils import timezone
from django.conf import settings
from django import forms
# Create your models here.
FORM_CHOICES = [
('very bad', 'very bad'),
('bd', 'bad'),
('OK', 'OK'),
('good', 'good'),
('very good', 'very good'),
]
class DiaryEntry(models.Model):
"""
Define the diary entry model here
"""
author = models.ForeignKey(settings.AUTH_USER_MODEL) # link author to the registered user
title = models.CharField(max_length=200) # set this to be the date later on
created_date = models.DateTimeField(auto_now_add=True)
body = models.IntegerField(blank=True, null=True, choices=FORM_CHOICES)
mind = models.IntegerField(blank=True, null=True, choices=FORM_CHOICES)
insights = models.TextField()
def publish(self):
self.save()
def __unicode__(self):
return self.title
提前谢谢了。