2016-12-28 55 views
0

我需要我的django項目幫助。我需要一個按鈕「保存並創建」模板中的表單。如何在Django中創建按鈕'保存並創建'?什麼是views.py?模板中有什麼?Django'保存並創建新的'按鈕。怎麼樣?

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> 
+0

請在這裏發表您的代碼樣本!告訴我們你的成績有多遠 –

+0

不在這裏......在你的問題中。請編輯它,並通過您的代碼..謝謝 –

+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> 
相關問題