Django: form id for active use

I'm not sure the name describes what I want for sure. I want to achieve something similar: Django add / remove form without multiple submission .

But I do not have a list of elements, I have a set of forms and forms. The form of this form set contains information that I could use to create such a link {% url 'item_edit' item.id%}. The problem is that this is the value of the hidden field. Here ( http://docs.djangoproject.com/en/dev/topics/forms/#looping-over-the-form-s-fields ) you have a list of options for using form fields in the template, but none of them is {{field.value}}. If I tried this, then it just failed.

Anyway. to the code. What I have in the template:

    <form enctype="multipart/form-data" method="post" action="/list/edit/{{ list.id }}/">
        <table>
        {{ form.as_table }}
        {{ formset.management_form }}
        {% for form in formset.forms %}
            {% if forloop.first %}
                <tr>
                {% for field in form.visible_fields %}
                    <td>{{ field.label }}</td>
                {% endfor %}
                </tr>
            {% endif %}
            <tr>
                {% for field in form.visible_fields %}
                    {% if not forloop.last %}
                        <td>{{ field }}</td>
                    {% else %}
                        <td>{{ field }}
                    {% endif %}
                {% endfor %}
                {% for field in form.hidden_fields %}
                    {% if not forloop.last %}
                        {{ field }}
                    {% else %}
                        {{ field }}</td>
                    {% endif %}
                {% endfor %}
            </tr>
        {% endfor %}
        <tr><td><input type="submit" value="Submit"></td><td colspan="4">&nbsp;</td></tr>
        </table>
    </form>

And this gives me inline string lines as follows:

<tr>
    <td><input type="text" maxlength="200" value="test2" name="shoppinglistitem_set-0-itemname" id="id_shoppinglistitem_set-0-itemname"/></td>
    <td><input type="text" maxlength="200" value="http://www.xxx.ee" name="shoppinglistitem_set-0-link" id="id_shoppinglistitem_set-0-link"/></td>
    <td><input type="text" maxlength="100" value="eepöäsdöäfsdfd" name="shoppinglistitem_set-0-store" id="id_shoppinglistitem_set-0-store"/></td>
    <td><input type="text" id="id_shoppinglistitem_set-0-price" value="22134" name="shoppinglistitem_set-0-price"/></td>
    <td><input type="checkbox" id="id_shoppinglistitem_set-0-DELETE" name="shoppinglistitem_set-0-DELETE"/><input type="hidden" id="id_shoppinglistitem_set-0-list" value="1" name="shoppinglistitem_set-0-list"/><input type="hidden" id="id_shoppinglistitem_set-0-listitem_ptr" value="5" name="shoppinglistitem_set-0-listitem_ptr"/></td>
</tr>

and I'm looking for a way to add a link like this

<a href={% url 'remove_list_item' item.id %}>REmove</a> 

or simply

<a href="http://localhost/list/removeitem/{{ id }}">REmove</a>

Urlconf for this view:

url(r'^removeitem/(?P<lisitem_id>\d+)/$', 'remove_list_item', name='remove_list_item')

So, is there an easy way to get this element (object) identifier from the form? Do I need to create a widget for this link to delete?

Alan.

+3
source share
2 answers

First of all, you should not use links (GET requests) to trigger actions that modify or delete data, you should use POST requests.

- , JavaScript -, AJAX POST. JavaScript . JavaScript ( ), (, ).

, Djangos CSRF-Middleware . CSRF-Middleware .

Django Form , , , , initial. form.initial["id"] {{ form.initial.id }}, , .

+7

for, , , , , , , , (, )

, formet

for

{% for form, data in forms_and_data_list % }
{% endfor %}

, - URL- , .

0

Source: https://habr.com/ru/post/1717504/


All Articles