Django“动态”验证表(更新) [英] Django "dynamic" verification form (updated)

查看:142
本文介绍了Django“动态”验证表(更新)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试在Django中创建一个验证表单,向用户提供一个选择列表,其中只有一个是有效的。一个最喜欢的披萨浇头包括菠萝和羊角的用户 -


你最喜欢的比萨饼顶部是什么?




  • Ham

  • 菠萝

  • Anchovie


每次选择都应该是顺序和值两种,但总是有一个有效的答案。例如,如果用户刷新页面,表单可能是 -


你最喜欢的比萨饼顶部是什么?




  • Mozarella

  • Ham

  • Roquefort


如何构建这样的表单?我被沼泽我们必须记录某个具体形式的有效选择:




  • 在每个问题(不可缩放)的数据库中创建条目?

  • 在会话中设置(hacky)?

  • 根据用户ID(可以预测)?



修改 - 我们在数据库中存储比萨饼的列表 -

  class UserProfile(Model):
favourite_pizza_toppings = ManyToManyField(PizzaTopping)

我想要的是一种形式,如 -

 类QuestionForm(Form):
questions = ChoiceField (选择=(('a',RANDOMLY_CHOSEN_OPTION_A),
('b',RANDOMLY_CHOSEN_OPTION_B),
('c',RANDOMLY_CHOSEN_OPTION_C))
pre>

其中,每次实例化时,所有 RANDOMLY_CHOSEN_OPTION PizzaTopping.objects.all ,只有一个是 PizzaTopping 也在 UserProfile.favourite_pizza_toppings



我们需要验证用户POST时的表单 - 这是棘手的一点。我们必须以某种方式记录哪个是有效的选择( a b c )。我们如何做到这一点?



谢谢

解决方案

init 方式构建选项,否则表单元素选择将在每次加载页面时都相同(它们不会更改)。

  class QuestionForm(Form):
questions = ChoiceField()

def __init __(self,* args,** kwargs) :
super(QuestionForm,self).__ init __(* args,** kwargs)
self.fields ['questions']。choices = set_up_choices()#创建列表的函数

clean_questions(self):
#你的验证问题选择
#这里你可以检查选项是否匹配
#一个正确的值,如果没有抛出一个表单验证异常
return self.cleaned ['questions']


I'm trying to create a verification form in Django that presents a user with a list of choices, only one of which is valid.

For example, for a user whose favourite pizza toppings includes pineapple and roquefort—

What is your favourite pizza topping?

  • Ham
  • Pineapple
  • Anchovie

The choices should be random every time, in both order and values, but always have one valid answer. For example, if the user refreshes the page, the form might be—

What is your favourite pizza topping?

  • Mozarella
  • Ham
  • Roquefort

How do I build such a form? I'm swamped. We have to record somewhere what the valid choice is for a specific form:

  • Create entries in the database for each question (not scalable)?
  • Set it in the session (hacky)?
  • Based on user ID (could be predictible)?

Edit—We do store the list of pizza toppings in the database—

class UserProfile(Model):
    favourite_pizza_toppings = ManyToManyField(PizzaTopping)

What I want is a form such as—

class QuestionForm(Form):
    questions = ChoiceField(choices=(('a', RANDOMLY_CHOSEN_OPTION_A),
                                     ('b', RANDOMLY_CHOSEN_OPTION_B),
                                     ('c', RANDOMLY_CHOSEN_OPTION_C)))

Where, each time it's instantiated, all RANDOMLY_CHOSEN_OPTION are chosen randomly from PizzaTopping.objects.all, with only one being a PizzaTopping also in UserProfile.favourite_pizza_toppings.

We'll need to verify the form when the user POSTs it though—and that's the tricky bit. We have to somehow record which is the valid choice (a, b, or c) for each instantiated form. How can we do this?

Thanks

解决方案

You will need to build up the choices in the the form init method, otherwise the form elements choices will be the same each time the page loads (they won't change).

class QuestionForm(Form):
    questions = ChoiceField()

    def __init__(self, *args, **kwargs):
        super(QuestionForm, self).__init__(*args, **kwargs)
        self.fields['questions'].choices = set_up_choices() # function that creates list

    clean_questions(self):
        # do your validation of the question selection
        # here you could check whether the option selected matches
        # a correct value, if not throw a form validation exception
        return self.cleaned['questions']

这篇关于Django“动态”验证表(更新)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆