Django'保存并创建新'按钮。怎么样?

时间:2016-12-28 06:54:49

标签: django django-templates

我的django项目需要帮助。我在模板中需要一个“保存并创建”按钮。我如何在Django中创建按钮'保存并创建'? views.py 是什么?模板中有什么用?

models.py

class Person(models.Model):
    name = models.CharField()
    last_name = models.CharField()

forms.py

class PersonForm(forms.ModelForm):
    class Meta:
        model = Person
        fields = ['name', 'last_name',]

views.py

def new_person(request):
    if request.method == "POST":
        person_form = PersonForm(request.POST or None)
        if person_form.is_valid():
            person_f.save()
            messages.success(request, 'Success!')
            return redirect('all_person',)
    else:
        person_form = PersonForm()
    context = {
        'person_form': person_form,
        'persons': Person.objects.all().order_by('last_name'),
    }
    return render(request, 'forms/add_person_and_edit.html', context)

形式/ add_person_and_edit.html

<form>
    {% csrf_token %}
    {{ person_form.as_p }}
    <input type="submit" value="Save">
</form>

1 个答案:

答案 0 :(得分:0)

您可以覆盖此django模板

/templates/admin/submit_line.html

{% load i18n admin_urls %}
<div class="submit-row">
{% if show_save %}<input type="submit" value="{% trans 'Save' %}"   class="default" name="_save" {{ onclick_attrib }}/>{% endif %}
{% if show_delete_link %}<p class="deletelink-box"><a href="{% url opts|admin_urlname:'delete' original.pk|admin_urlquote %}"     class="deletelink">{% trans "Delete" %}</a></p>{% endif %}
{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" {{ onclick_attrib }}/>{%endif%}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" {{ onclick_attrib }}/>{% endif %}
{% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" {{ onclick_attrib }}/>{% endif %}

<input type="submit" value="{% trans 'New button 1' %}" name="_button1" {{ onclick_attrib }}/>
<input type="submit" value="{% trans 'New button 2' %}" name="_button2" {{ onclick_attrib }}/>
</div>