如何在Django中通过url传递参数? [英] How do I pass parameters via url in django?

查看:685
本文介绍了如何在Django中通过url传递参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将参数传递给我的视图,但我不断收到此错误:

I am trying to pass a parameter to my view, but I keep getting this error:

NoReverseMatch at /pay/how

Reverse for 'pay_summary' with arguments '(False,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['pay/summary/$']

/ pay /方式是我当前的视图。 (即该视图返回的当前模板。)。

/pay/how is the current view that I'm at. (that is the current template that that view is returning).

urls.py

url(r'^pay/summary/$', views.pay_summary, name='pay_summary')

views.py

def pay_summary(req, option):
    if option:
        #do something
    else:
        #do something else
    ....

模板

<a href="{% url 'pay_summary' False %}">my link</a>

编辑

我希望视图应该接受POST请求,而不是GET。

I want the view should accept a POST request, not GET.

推荐答案

您需要在url上定义一个变量。例如:

You need to define a variable on the url. For example:

url(r'^pay/summary/(?P<value>\d+)/$', views.pay_summary, name='pay_summary')),

在这种情况下,您可以致电 pay / summary / 0

In this case you would be able to call pay/summary/0

通过替换可以是真/假字符串\d + \s + ,但是您需要解释字符串,但这不是最好的。

It could be a string true/false by replacing \d+ to \s+, but you would need to interpret the string, which is not the best.

然后可以使用:

<a href="{% url 'pay_summary' value=0 %}">my link</a>

这篇关于如何在Django中通过url传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆