简体   繁体   English

如何在 Django 模板中的动态 URL 中传递变量

[英]How can I pass variable in dynamic URL in Django Template

So I have a blog site and I want that when someone clicks on the delete button so the blog should delete.所以我有一个博客网站,我希望当有人点击删除按钮时,博客应该删除。

But the slug in the URL is a variable how I can pass it?但是 URL 中的 slug 是一个变量,我如何传递它?

URL path: path('deletePost/<slug:slug>', views.deletePost, name='deletePost'), URL路径: path('deletePost/<slug:slug>', views.deletePost, name='deletePost'),

I have passed the variable Del_slug to this view so how can I use it in the URL.我已将变量Del_slug传递给此视图,因此如何在 URL 中使用它。

<a href="{% url 'deletePost' Del_slug %}">Delete</a>

for my case that also worked对于我的情况也有效

<a  href="{% url 'deletePost' object.id %}">Delete</a> 

where url pattern is网址模式在哪里

 url=[
   path('deletePost/<int:pk>', views.deletePost, name='deletePost'),
    ]

or或者

You can use the add template filter:您可以使用添加模板过滤器:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM