简体   繁体   中英

Django Model Formsets and named urls

I'm having some problems with model formsets in django.

# forms.py
OppFormSet = modelformset_factory(Opportunity, fields=opp_fields)

# views.py

def index2(request):
    queryset = Opportunity.objects.for_user(request.user).filter(is_deleted=False)

    if request.method == 'POST':
        formset = OppFormSet(request.POST, queryset=queryset)

    else:
        formset = OppFormSet(queryset=queryset)

    return render_to_response("opp_index2.tmpl", { "formset" : formset}, context_instance=RequestContext(request))

Here are my 2 questions regarding model forms:

  1. In my template I have {% for form in formset %} to create the forms in a table, but somehow, I keep getting an extra blank row, as if there is an additional blank form in the formset.

  2. If i use {{ form.instance.id }} to output an id while iterating through the formset, it prints out ok. However, if I do a {% url summary form.instance.id %} I keep getting an error regarding no NoReverseMatch with arguments (None,). It seems like when using form.instance.id in a template tag, it doesn't work. Is that true? If so, how do i circumvent it?

Thank you in advance.

Use {% for form in formset.forms %} This "blank form" is ManagementForm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM