How do I pass parameters via url in django?

前端 未结 2 534
抹茶落季
抹茶落季 2021-01-02 12:14

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,)\         


        
相关标签:
2条回答
  • 2021-01-02 12:53

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

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

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

    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.

    You can then use:

    <a href="{% url 'pay_summary' value=0 %}">my link</a>
    
    0 讨论(0)
  • 2021-01-02 12:58

    To add to the accepted answer, in Django 2.0 the url syntax has changed:

    path('<int:key_id>/', views.myview, name='myname')
    

    Or with regular expressions:

    re_path(r'^(?P<key_id>[0-9])/$', views.myview, name='myname')
    
    0 讨论(0)
提交回复
热议问题