Django CreateView 取消空字段

Django CreateView cancel with empty fields

我的 Country 模型有一个 CountryCreateView,我正在尝试向其添加取消按钮。但是,如果没有在每个字段中填写任何内容并且我按下取消按钮,我会看到一个弹出窗口,显示 Please fill out this field。当我按下取消按钮时,我不关心字段中有什么,我只想转到我要重定向到的页面。最重要的是,我希望此字段非要求仅适用于此。我不想让其他任何东西允许空白字段。

views.py:

class CountryCreateView(generic.edit.CreateView):
    model = Country
    fields = ['code', 'name']
    template_name_suffix = '_create'

    def post(self, request, *args, **kwargs):
        if "cancel" in request.POST:
            return HttpResponseRedirect(reverse('appName:country_list'))
        else:
            return super(CountryCreateView, self).post(request, *args, **kwargs)

models.py:

class Country(AutoUpdateModel): # extends models.Model

    code = models.CharField(primary_key=True, max_length=2, validators=[validate_nonempty, validate_string_length_two, validate_string_all_caps]) 
    name = models.CharField(max_length=50, db_column="Name", validators=[validate_nonempty])
    # etc...

country_create.html:

{% extends "appName/base.html" %}


{% block content %}


    <h2 class="title">Create a new Country:</h2>

    <form action="" method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Save" class="btn btn-primary"/>
        <input type="submit" name="cancel" value="Cancel" class="btn btn-secondary"/>
    </form>

{% endblock %}

请检查这是否有效 - 在 html

中添加 formnovalidate 提交按钮
<input type="submit" formnovalidate name="cancel" value="Cancel" class="btn btn-secondary"/>