Django:返回带有POST参数的模板(Django: Returning a template with POST parameters)

编程入门 行业动态 更新时间:2024-10-25 04:18:36
Django:返回带有POST参数的模板(Django: Returning a template with POST parameters)

我有一个包含多行的表,每行都有一个“编辑”按钮。 单击此按钮后,用户应重定向到已预先填写其详细信息的表单。 现在,我在“编辑”按钮的定义中定义了数据属性:

<a href='#' class="edit-user" data-email="abc@gmail.com">edit</a>

使用JQuery,我将导出'email'数据属性的值并将其传递给/add-edit-user的POST请求,该/add-edit-user也映射到具有我的表单的页面:

$( ".edit-user" ).click(function() { var url = base_url + '/add-edit-user/'; var data = { 'email': $(this).data('email'), 'csrfmiddlewaretoken': csrftoken, }; // $.post(url, data); $.post(url, data).done(function(result) { console.log(result); }).fail(function(error) { console.log(error); }); });

/add-edit-user URL映射到以下视图,我正在尝试定义表单对象并将其作为上下文变量传递给我的模板文件:

def add_edit_users(request): form = AddUpdateForm(request.POST or None) data = {'form': form} return render(request, 'add_edit_users.html', data)

现在,我的假设是,如果用户点击“编辑”按钮,我会在数据属性中设置'email'参数,并在我的POST请求/add-edit-user其传递给/add-edit-user URL,之后我我将使用表单对象呈现我的表单模板作为上下文变量,该变量将显示已预填充email字段的表单。

但是,在单击edit链接时,页面不会重定向到表单页面,但我会在响应中获取整个表单页面的HTML。 如何将流重定向到表单的模板,以便从POST变量预填充输入字段?

I have a table with multiple rows, each of which has an 'edit' button. On clicking this button, the user should redirect to a form with his details already prefilled. Now, I have defined data attributes inside the 'edit' button's definition:

<a href='#' class="edit-user" data-email="abc@gmail.com">edit</a>

Using JQuery, I'm deriving the value of the 'email' data attribute and passing it in a POST request to /add-edit-user, which is also mapped to the page which has my form:

$( ".edit-user" ).click(function() { var url = base_url + '/add-edit-user/'; var data = { 'email': $(this).data('email'), 'csrfmiddlewaretoken': csrftoken, }; // $.post(url, data); $.post(url, data).done(function(result) { console.log(result); }).fail(function(error) { console.log(error); }); });

The /add-edit-user URL maps to the following view where I'm trying to define my form object and pass on as a context variable to my template file:

def add_edit_users(request): form = AddUpdateForm(request.POST or None) data = {'form': form} return render(request, 'add_edit_users.html', data)

Now, my assumption was that if the user clicks on the 'edit' button, I'd set the 'email' parameter in a data attribute and pass it on in my POST request to /add-edit-user URL, after which I'll render my form template with the form object as a context variable which will display the form with email field already prefilled.

However, on clicking of the edit link, the page is not redirected to the form page, but I do get the entire form page's HTML in my response. How can I redirect the flow to my form's template so that my input fields are prefilled from the POST variables?

最满意答案

您想要实现的目标可以通过创建这样的表单来完成:

<form method='post' action='/add-edit-user/'> {% csrf_token %} <input type="email" name="email" value="abc@gmail.com" hidden> <button class="edit-user" type="submit">Edit</button> </form>

此表单将发布数据中的电子邮件字段的值发送到url: / add-edit-user / 。 导致预先填充电子邮件字段重定向到网址。

What you want to achieve can be done by creating a form like this:

<form method='post' action='/add-edit-user/'> {% csrf_token %} <input type="email" name="email" value="abc@gmail.com" hidden> <button class="edit-user" type="submit">Edit</button> </form>

This form sends value of the email field in post data to the url : /add-edit-user/. Resulting in redirecting to the url with the email field prefilled.

更多推荐

本文发布于:2023-07-28 19:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1308189.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   参数   POST   Django   template

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!