django - 如果提交的表单值为X,则阻止表单提交

时间:2014-12-21 22:53:35

标签: python django django-forms django-views

我正在使用Django 1.4.6& python 2.7。

我有一个测试表单,允许用户从选择列表中输入数据:

<select name="language_code" id="id_language_code">
    <option value="en-CA">English (Canada) - English (Canada)‎</option>
    <option value="en-GB" selected="selected">English (UK) - English (UK)‎</option>
    <option value="en">English (US)</option>
    <option value="fr-CA">French (Canada) - français (Canada)‎</option>
    <option value="fr">French (France) - français (France)‎</option>
</select>

表单提交工作正常。

但是,我需要进行更改,以便如果用户选择fr-CA的选择列表选项然后提交表单,那么我想不添加记录并将用户重定向到其他表单 - 返回重定向(settings.MENU_DETAIL_LINK_NAME_DETAILS)。

基本上,如果LanguageVersion模型的布尔字段language_code_disabled为true,则用户不应该添加语言版本。

我不确定如何做到这一点。我一直在四处乱逛,所以我对如何实现这一点感到困惑。

这是我的模型代码:

class NameDetails(models.Model, FillableModelWithLanguageVersion):
    user = models.ForeignKey(User)
    language_version = models.ForeignKey('LanguageVersion')
    name_details_prefix_title = models.CharField(null=True, blank=True, max_length=25)
    name_details_first_name = models.CharField(null=False, blank=False, max_length=50)
    name_details_middle_name = models.CharField(null=True, blank=True, max_length=100)
    name_details_last_name = models.CharField(null=False, blank=False, max_length=60)
    name_details_suffix_title = models.CharField(null=True, blank=True, max_length=25)

class LanguageVersion(models.Model):
    """Language version selection for a user"""
    user = models.ForeignKey(User)
    language_code = models.CharField(max_length=32)
    language_code_disabled = models.BooleanField(default=False)

这是我的views.py

@login_required
def name_details_add(request):
    language_versions = LanguageVersion.objects.filter(user=request.user)
    available_languages = get_available_language_details(language_versions, request.user.userprofile.language_preference)
    name_details_num = request.user.namedetails_set.count()
    preview_labels = get_name_details_labels(available_languages)
    if name_details_num >= settings.MAX_NAME_DETAILS:
        return redirect(settings.MENU_DETAIL_LINK_NAME_DETAILS)
    if request.method == 'GET':
        form = NameDetailsForm(
            available_languages=available_languages,
            language_preference=request.user.userprofile.language_preference,)
    elif request.method == 'POST':
        form = NameDetailsForm(
            available_languages=available_languages,
            language_preference=request.user.userprofile.language_preference,
            data=request.POST)
        if form.is_valid() and name_details_num < settings.MAX_NAME_DETAILS:
            name_detail = NameDetails(user=request.user)
            name_detail.fill(form.cleaned_data)
            name_detail.save()
            messages.success(request, _('successfully added.'))
            return redirect(settings.MENU_DETAIL_LINK_NAME_DETAILS)

我希望somone可以帮助我解决这个问题。

我需要了解如何从language_code_disabled为True的用户的LanguageVersions模型中调用所有语言代码的列表。

我是否编写了一个循环语言版本的函数,还是可以编写一个简单的调用函数来返回禁用语言代码列表,例如en,fr,fr-CA,de?

disabled_language_versions = LanguageVersion.objects.filter(user=request.user, language_code_disabled=True)

2 个答案:

答案 0 :(得分:2)

您可以在表单上为LanguageVersion fk添加字段验证,并在视图中访问该信息:

class NameDetailsForm(forms.ModelForm):
    ...
    def clean_language_version(self):
        lang = self.cleaned_data.get('language_version')
        if LanguageVersion.objects.filter(language_code=lang, language_code_disabled=True).exists():
            raise forms.ValidationError("Language code disabled")
        return lang

然后在视图中:

if form.is_valid() ... :
    ...
elif form['language_version'].errors:
    return redirect(settings.MENU_DETAIL_LINK_NAME_DETAILS)

答案 1 :(得分:1)

嗯,你可以这样做:

if request.POST['language_code'] == 'fr-CA':
    return HttpResponseRedirect('/some/other/page/')