首页 > 解决方案 > 我想将两个 id 从 .html 发布到视图

问题描述

html 文件我想把两个 id 带到views.py 我能得到吗?

path('yorum_sil/<int:id>',views.yorum_sil,name="yorum_sil")

我想做如下代码

path('yorum_sil/<int:comment_id> and <int:post_id>',views.yorum_sil,name="yorum_sil")

标签: pythondjangourl

解决方案


这是可能的,尽管您不应该使用这样的空格。例如,您可以使用如下斜杠:

path('yorum_sil/<int:comment_id>/<int:post_id>',views.yorum_sil,name="yorum_sil")

您的视图函数(此处yorum_sil)当然需要接受这两个参数,例如:

# app/views.py

def yorum_sil(request, comment_id, post_id):
    # ...
    return ...

如果执行反向查找,则需要传递这两个参数。例如在这样的模板中:

<a href="{% url 'yorum_sil' comment_id=14 post_id=25 %}">some_link</a>

如果我正确翻译了 yorum sil,则表示删除评论,请注意通常 GET 请求不应副作用。为了删除/创建/...一个实体,您应该使用 POST 请求。


推荐阅读