[英]Getting text of HTML element and passing it to django view using AJAX
I have the following paragraph: <p>{{donation.pk}}</p>
.我有以下段落:
<p>{{donation.pk}}</p>
。
I want to pass the value of the paragraph into the view bellow as a pk
我想将段落的值作为
pk
传递到下面的视图中
def acceptdonation(request, pk): #the `pk` should be the `pk` of the `p` element
deleteitem = Donation.objects.filter(id = pk)
deleteitem.delete()
return redirect('/dashboard/')
I did research, and heard that you need to use AJAX.我做了研究,听说你需要使用 AJAX。
How exactly can I achieve this.我究竟如何才能做到这一点。 I am not to good with javascript so the easiest and most minimalist way would be helpful.
我不擅长使用 javascript,所以最简单和最简约的方法会有所帮助。
PLEASE NOTE: I already have a button that calls the view, I just want to pass the value of the p
element into my function请注意:我已经有一个调用视图的按钮,我只想将
p
元素的值传递到我的函数中
Assuming that the path of your view is something like:假设您的视图路径类似于:
path('acceptdonation-path/<int:pk>/', views.acceptdonation, name='acceptdonation-view'),
You can use the name of the path to get the URL in your template:您可以使用路径名称来获取模板中的 URL:
<a href="{% url 'acceptdonation-view' pk=donation.pk %}">click</a>
Then just simply custom the link to look like a button with CSS, or if you really want to use a button, you can do something like:然后只需简单地将链接自定义为具有 CSS 的按钮,或者如果您真的想使用按钮,您可以执行以下操作:
<form action="{% url 'acceptdonation-view' pk=donation.pk %}">
<button type="submit" value="click" />
</form>
No AJAX request is needed here, just because, at the end of your request, there is a redirection that is not commonly used in AJAX requests, so a simple request should fit good here.这里不需要 AJAX 请求,只是因为在您的请求结束时,有一个在 AJAX 请求中不常用的重定向,所以一个简单的请求应该适合这里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.