1
我在進行選擇並點擊提交後收到此錯誤消息。我不確定它是如何修復它的。我試圖在頁面上的表單中隱藏HiddenInput。是否有另一種方式做到這一點比在forms.py錯誤:(隱藏的字段)選擇一個有效的選擇。 (Django)
Models.py
from django.db import models from django.utils import timezone
from django import forms
from django.forms import ModelForm
from django.contrib.auth.models import User
from random import randrange
class Type(models.Model):
type = models.CharField(max_length=50)
def _unicode__(self):
return self.type
class WordManager(models.Manager):
def random(self, type):
words = Word.objects.filter(type__type=type)
return words[randrange(len(words))]
class Word(models.Model):
dict = models.CharField(max_length=200)
type = models.ForeignKey(Type)
objects = WordManager()
def __unicode__(self):
return u'%s %s' % (self.dict, self.type)
class Question(models.Model):
question = models.CharField(max_length=200)
def __unicode__(self):
return self.question
class Meta(models.Model):
user = models.CharField(max_length=200)
time = models.DateTimeField('date published')
class Result(models.Model):
question = models.ForeignKey(Question)
word = models.ManyToManyField(Word)
user = models.ForeignKey(User)
score = models.IntegerField()
Forms.py
from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
from quest.models import Result, Question, Word
class ResultForm(ModelForm):
CHOICES = (
('1', '1'), ('2','2'), ('3','3'), ('4', '4'), ('5', '5')
)
question = forms.ModelChoiceField(queryset=Question.objects.all(), widget=forms.HiddenInput())
word = forms.ModelChoiceField(queryset=Word.objects.all(), widget=forms.HiddenInput())
user = forms.ModelChoiceField(queryset=User.objects.all(), widget=forms.HiddenInput())
score = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect())
class Meta:
model = Result
class UserForm(ModelForm):
class Meta:
model = User
fields = ('username',)
Views.py
from django.contrib.auth import authenticate, login
from django.core.urlresolvers import reverse
from django.shortcuts import render, render_to_response, HttpResponseRedirect
from django.template import RequestContext
from django.utils import timezone
from random import randrange
import re
from quest.models import Word, Type, Question, Meta
from quest.forms import ResultForm, UserForm
def index(request):
state = "Enter ID and password below"
username = password = ''
if request.POST:
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
state = "You're successfully logged in!"
else:
state = "Your account is disabled."
else:
state = "Your username and password were incorrect."
return render_to_response('index.html',{'state':state, 'username': username}, context_instance=RequestContext(request))
def question(request):
# Find all words to be replaced from the question marked by '$'
question = Question.objects.get(id=1)
word_types = re.findall(r"\w+[$]", question.question)
word_types = [find_w.rstrip("$") for find_w in word_types]
words = [Word.objects.random(type) for type in word_types]
question_text = question.question.replace("$", "")
for word in words:
question_text = question_text.replace(word.type.type, word.dict)
for word in words:
question_text = question_text.replace(word.type.type, word.dict)
if request.method == 'POST':
form = ResultForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('question'))
else:
for error in form.errors:
print error
else:
form = ResultForm(initial={'question': question.id, 'word': [word.id for word in words], 'user': request.user.id})
return render_to_response('quest/question.html', {'final': question_text, 'form': form}, context_instance=RequestContext(request))
相反字= form.ModelChoiceField(...),問題是由添加字解= form.ModelMultipleChoiceField(...)。我原本以爲這個小部件提供了這個問題。 – ono
@ ono3如果您確實需要在字段中存儲多個值,那麼是的,ModelMultipleChoiceField是正確的字段。我不知道那是你的意圖。 – rantanplan
我想我對此並不清楚。感謝您的輸入。 – ono