Optional get parameters in django?

后端 未结 7 1639
后悔当初
后悔当初 2020-12-05 00:28

Can someone please explain how you can write a url pattern and view that allows optional parameters? I\'ve done this successfully, but I always break the url template tag.

相关标签:
7条回答
  • 2020-12-05 01:26

    For anyone who is still having this problem. I use Django 1.5 (updated: using 1.8) and it's still working fine.

    I use:

    urls.py

    url(r'^(?P<app_id>\d+)/start/+(?P<server_id>\d+)?', views.restart, name='restart')
    

    Then when I want to have the two urls

    /1/start/2
    

    and

    /1/start
    

    I use:

    {% url '<namespace>:start' app.id %}
    {% url '<namespace>:start' app.id server.id %}
    

    This will create the urls

    /1/start/2 and 
    /1/start/ <- notice the slash.
    

    If you create a url manually you have to keep the / in mind.

    I hoop this helps anyone!

    0 讨论(0)
提交回复
热议问题