Django:从表单集中获取表单ID

发布于 2024-08-05 03:06:33 字数 3421 浏览 2 评论 0原文

我不确定标题是否准确地描述了我想要的内容。我想要的是实现类似的目标: Django 添加/删除无需多次提交的表单

但我没有我有表单集和表单的项目列表。此表单集的形式确实包含我可用于创建类似 {% url 'item_edit' item.id %} 的链接的信息。问题在于它是隐藏字段的值。这里(http://docs .djangoproject.com/en/dev/topics/forms/#looping-over-the-form-s-fields)您有一个如何在模板中使用表单字段的选项列表,但没有一个它们是 {{ field.value }}。如果我尝试这样做,那么它就会默默地失败。

反正。到代码。我在模板中的内容是:

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

这给了我这样的内联表单行:

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

我正在寻找某种方法来添加这样的链接

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

或只是

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

此视图的 Urlconf 是:

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

那么有没有一些简单的方法来获取项目的 id (对象)来自形式?我是否必须为该删除链接创建某种小部件?

艾伦.

I am not sure if title describes what i want accurately. What i want is to achieve something like that: Django add / remove form without multiple submit.

But i have not list of items i have formset and forms. The form of this formset does contain information i could use for creating link like that {% url 'item_edit' item.id %}. The problem is that it is a value of an hidden field. Here (http://docs.djangoproject.com/en/dev/topics/forms/#looping-over-the-form-s-fields) you have a list of options how to use fields of a form in a template, but none of them is {{ field.value }}. If i tried that, then it just failed silently.

Anyway. to the code. What i have in template is:

    <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"> </td></tr>
        </table>
    </form>

And this gives me inline form rows like this:

<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 am looking for some way to add link like this

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

or just

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

Urlconf for this view is:

url(r'^removeitem/(?P<lisitem_id>\d+)/

So is there some easy way to get that id of the item(object) from the form? Do i have to create some kind of widget for that remove link instead?

Alan.

, 'remove_list_item', name='remove_list_item')

So is there some easy way to get that id of the item(object) from the form? Do i have to create some kind of widget for that remove link instead?

Alan.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天生の放荡 2024-08-12 03:06:33

首先,您不应该使用链接(GET 请求)来触发编辑或删除数据的操作,您应该使用 POST 请求。

您仍然可以使用链接来删除数据,方法是通过 JavaScript 创建链接并使用其单击事件发出 Ajax POST 请求。使用 JavaScript,您还可以轻松地从隐藏字段读取 id。如果您的表单应该可以在没有 JavaScript 的情况下使用(它应该),那么您应该创建另一个表单来删除项目(可能只是一个删除按钮)。

您应该使用表单进行删除,因为要使 Django CSRF 中间件工作,您需要表单。 CSRF-Middleware 应该位于您的中间件堆栈中。

如果您使用对象初始化 Django 表单(您可能已经这样做了),则该对象的字段将存储在名为initial 的字典中。您也许可以通过 form.initial["id"] 或在模板中访问它 {{ form.initial.id }},但我不确定是否可以它有效或者它是否是一个好主意。

First of all, you shouldn't use links (GET requests) to trigger actions that edit or delete data, you should use POST requests.

You can still use a link to delete data by creating one via JavaScript and using their click-Event to make an Ajax POST request. With JavaScript you can also easily read the id from the hidden field. If your form should be usable without JavaScript (and it should), then you should create another form for deleting items (probably just a delete button).

You should use a form for deleting, because for Djangos CSRF-Middleware to work you need forms. And CSRF-Middleware should be in your middleware stack.

If you initialize a Django Form with an object, as you probably have, the fields of the objects are stored in a dictionary called initial. You might be able to access it via form.initial["id"] or in template speak {{ form.initial.id }}, though I am not sure if it works or if it's a good idea.

烛影斜 2024-08-12 03:06:33

您在模板中使用的 for 循环还可以处理列表或元组的列表以及简单列表,因此我使用混合显示和表单集元素来解决此问题的解决方案是在我的视图中创建元组列表。每个元组都是(表单,数据)

然后我将表单和数据的混合列表传递给模板而不是简单的表单集

然后模板中的外部 for 循环就变成了

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

您可以显示数据部分,在您的情况下是编辑 url,就像平常一样。

The for loop you are using in the template can also handle lists of lists or tuples as well as simple lists so the solution I use to this problem, mixing display and formset elements, is to create a list of tuples in my view. Each tuple is (form, data)

I then pass this mixed list of forms and data to the template rather than simply the formset

The outer for loop in your template then becomes

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

you can then display the data part, in your case the edit url, just as you would normally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文