当前位置: 首页>>代码示例>>Python>>正文


Python CharField.required方法代码示例

本文整理汇总了Python中django.forms.CharField.required方法的典型用法代码示例。如果您正苦于以下问题:Python CharField.required方法的具体用法?Python CharField.required怎么用?Python CharField.required使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.forms.CharField的用法示例。


在下文中一共展示了CharField.required方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: make_quiz_form

# 需要导入模块: from django.forms import CharField [as 别名]
# 或者: from django.forms.CharField import required [as 别名]
def make_quiz_form(quiz, select_to_radio=False):
    """
    Generates a form on the fly based on the Quiz questions
    """
    form_fields = OrderedDict()
    for question in quiz.get_questions():
        AnswerModel = question.get_answer_class()
        model_fields = fields_for_model(AnswerModel, exclude=['userprofile', 'review'])
        if AnswerModel == MultipleChoiceAnswer or AnswerModel == RatingAnswer:
            if select_to_radio or (quiz.question_widget == quiz.RADIO_WIDGET) or (question.widget == question.RADIO_WIDGET):
                model_fields = fields_for_model(
                    AnswerModel, exclude=['userprofile', 'review'], formfield_callback=multiplechoice_to_radio)
                # Begin Diry hack
                # for when radio select has for some reason not been set by fields_for_model
                # this happens for RatingAnswer objects
                # first we check if choices exist to avoid none choice fields
                if hasattr(model_fields['answer'], '_choices'):
                    # then we check if the widget is still Select
                    if isinstance(model_fields['answer'].widget, Select):
                        # we manually make it into a Radio Select field
                        model_fields['answer'].widget = RadioSelect()
                        # we remove the empty answer choice usually preset in Select Fields
                        if not model_fields['answer']._choices[0][0]:
                            model_fields['answer']._choices.pop(0)
                # End Diry hack

        answer_field = model_fields['answer']
        answer_field.required = question.required
        # answer_field.question = question ?? should this be included
        answer_field.has_image_answers = question.has_image_answers
        answer_field.widget_to_use = question.widget
        other_field = None
        if question.image:
            thumb_size = getattr(settings, 'QUESTION_LABEL_THUMBS_SIZE', "500x400")
            thumb = get_thumbnail(question.image.file, thumb_size)
            answer_field.label = format_html(
                "<img src='{thumb}' class='img-responsive question-label' alt='{qtitle}' title='{qtitle}' /><span class='question-text-latel'>{qtitle}</span>", thumb=thumb.url, qtitle=smart_str(question.title))
            if quiz.show_question_numbers:
                answer_field.label = format_html(
                    "<img src='{thumb}' class='img-responsive question-label' alt='{qtitle}' title='{qtitle}' /><span class='question-text-latel'>{qtitle}</span>", thumb=thumb.url, qtitle=smart_str("{}. {}".format(question.order, question.title)))
        else:
            this_label = question.title
            if quiz.show_question_numbers:
                this_label = "{}. {}".format(question.order, this_label)
            if question.description:
                answer_field.label = format_html("<span class='question-text-latel'>{}</span><div class='question-description'>{}</div>", smart_str(
                    this_label), mark_safe(smart_str(question.description).replace('\n', '<br />')))
            else:
                answer_field.label = smart_str(this_label)
        if question._meta.model == MultipleChoiceQuestion:
            answer_field.queryset = MultipleChoiceOption.objects.filter(question=question)
            if answer_field.queryset.filter(other=True).exists():
                other_field = CharField()
                other_field.widget.attrs['class'] = "other-field id_answer_{}".format(question.id)
                other_field.label = _("If you selected Other, please specify what you meant")
                other_field.required = False
        form_fields['answer_{}'.format(question.id)] = answer_field
        if other_field:
            form_fields['other_{}'.format(question.id)] = other_field
    return type('QuizForm', (BaseForm,), {'base_fields': form_fields})
开发者ID:moshthepitt,项目名称:answers,代码行数:62,代码来源:forms.py


注:本文中的django.forms.CharField.required方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。