Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 错误:(隐藏字段字)请选择有效选项。那个选择不是可用的选择之一_Django_Widget - Fatal编程技术网

Django 错误:(隐藏字段字)请选择有效选项。那个选择不是可用的选择之一

Django 错误:(隐藏字段字)请选择有效选项。那个选择不是可用的选择之一,django,widget,Django,Widget,我在选择并点击submit后收到此错误。我不确定它是如何修复的。我正试图在页面的表单中使用HiddenInput隐藏它。除了在forms.py上,还有别的方法吗 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 impo

我在选择并点击submit后收到此错误。我不确定它是如何修复的。我正试图在页面的表单中使用HiddenInput隐藏它。除了在forms.py上,还有别的方法吗

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))
如果没有人可以选择,那么成为一个选择领域又有什么意义呢? 好了,现在我知道你想做什么了。但下文仍然适用

还包括:

'word': [word.id for word in words]
在初始表单中,数据是错误的。ModelChoiceField提供了很多选择 但只能选择一个。所以“word”:someword.id应该是正确的

作为旁注,也许在如何从中选择随机数据方面会很有用
模型。

通过添加word=form.ModelMultipleChiceField…,而不是word=form.ModelChoiceField…,解决了此问题。。。。我最初认为小部件提供了问题。@ono3如果您确实希望在字段中存储多个值,那么是的,ModelMultipleChiceField是正确的字段。我不知道那是你的意图。
'word': [word.id for word in words]