模板中的“添加另一个表单”按钮(如django admin)

时间:2014-02-15 04:27:03

标签: python django

如何为渲染的formset创建“添加另一个____”按钮?

我注意到人们使用jQuery(Dynamically adding a form to a Django formset with Ajax

来解决这个问题

django民意调查教程并未真正解释如何在管理员中生成“添加其他选择”链接。 (https://docs.djangoproject.com/en/1.6/intro/tutorial02/

如何在模板,视图和广告中重复使用相同的功能?表格?

以下是我的一些代码。

forms.py

class StudentForm(forms.ModelForm):
    # The last 15 years.
    thisYear = int(time.strftime("%Y"))
    farAway = thisYear-15
    years = []
    for y in range(farAway,thisYear):
        years.append(str(y))

    birth_date = forms.DateField(widget=SelectDateWidget(years=tuple(years)))
    class Meta:
        model = Student
    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_method = 'post'
        self.helper.form_action = ''

        self.helper.add_input(Submit('submit', 'Submit'))
        super(StudentForm, self).__init__(*args, **kwargs)

添加-students.html

<h2>Add Students</h2>
    {% for f in StudentFormSet %}
        {% crispy f %}
    {% endfor %}

views.py

def addStudents(request):
    StudentFormSet = formset_factory(StudentForm, can_delete=True, extra=3)
    if request.method == 'POST':
        formset = StudentFormSet(request.POST)
        if formset.is_valid():
            saved = formset.save(commit=False)
            saved.save()
            messages.success(request, "Submitted! Thank you.")
    else:
        formset = StudentFormSet()
    return render_to_response('add-students.html', 
                                locals(),
                                context_instance=RequestContext(request))

0 个答案:

没有答案