开发者

Django formsets problem

开发者 https://www.devze.com 2022-12-26 19:28 出处:网络
I am trying to produce a template with many addresses (forms), where you can add, edit and remove them.

I am trying to produce a template with many addresses (forms), where you can add, edit and remove them.

Am I doing something wrong with formsets? Here are my views:

@login_required
def addresses(request):
    AddressesFormset = modelformset_factory(Address,
                                            can_delete = True,
                                            extra = 0,
                                            exclude = ['user'])

    log.debug('Queryset: %s', request.user.addresses.all())

    if request.method == 'POST':
        log.debug('Formset from POST')
        formset = AddressesFormset(request.POST)
        if formset.is_valid():
            log.debug('Saving form')
            formset.save()
            log.debug('Fromset from queryset')
            formset = AddressesFormset(queryset = request.user.addresses.all())
        else:
            log.debug('Form is not valid')
    else:
        log.debug('Fromset from queryset')
        formset = AddressesFormset(queryset = request.user.addresses.all())

    return render_to_response('accounts/addresses.html', locals(), context_instance = RequestContext(request))

@login_required
def add_address(request):
    address = Address.objects.create(user = request.user)
    address.save()
    return HttpResponseRedirect('/accounts/addresses/')

And template:

{{ formset.management_form }}
{% for form in formset.forms %}
    <table class="accountT">
        <tr  class="accountTT">
            <td><p>Ulica, nr domu, mieszkania:</p></td>
            <td>{{ form.street.errors }}{{ form.street }}</td>
        </tr>
        <tr  class="accountTT">
            <td><p>Miejscowość:</p></td>
            <td>{{ form.city.errors }}{{ form.city }}</td>
        </tr>
        <tr  class="accountTT">
            <td><p>Kod pocztowy:</p></td>
            <td>{{ form.zipcode.errors }}{{ form.zipcode }}</td>
        </tr>
        <tr 开发者_StackOverflow社区 class="accountTT">
            <td><p>Telefon kontaktowy:</p></td>
            <td>{{ form.phone.errors }}{{ form.phone }}</td>
        </tr>
        <tr>
            <td><p>Usuń:</p></td>
            <td>{{ form.DELETE }}</td>
        </tr>
        {{ form.id }}
    </table>
{% endfor %}

Edit: The problem is that adding a form I have to save the formset (in add_address()). I would like to see how do you treat formsets properly. I don't understand it at all ;).

Thanks in advance, Etam.


Well, you don't say what your problem is, but you are doing at least one thing wrong.

After confirming that the formset is valid, and then saving it, for some reason you then instantiate another formset and fall straight through to the render_to_response at the end of the function, so you end up displaying a set of blank forms again.

What you should do at that point is redirect somewhere else, eg to a confirmation page.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号