How do I pass variables in django through the url?

后端 未结 2 1045
野趣味
野趣味 2021-01-05 01:51

I am trying to pass a few variables but I am having some trouble and specifically have 3 questions. How do I encode the url string to take into account the special character

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

    Pass these variables as it is to template, there use url, before sending to template just do this in view.

    View.py

    related = urllib.quote(related, safe='')
    

    template

    <a href="{% url 'path.to.video_player' author video related %}" > <img src="img.png" > </a>
    

    Url.py

    url(r'^partner/(?P<author>[-\w]+)/(?P<video>\w+)/(?P<related>\w+)/$', 'video_player'),
    

    EDIT

    If you want to go without related parameter, or if there is doubt video can also be None then just do this in your view:

    def video_player(request, author, video=None, related=None):
    

    now you can use the url by

    <a href="{% url 'path.to.video_player' author video %}" > <img src="img.png" > </a>
    
    0 讨论(0)
  • 2021-01-05 02:20

    in newer versions of python you could simply just type: Example:

    path('<int:id>/delete/', delete_view, name = 'delete'),
    
    0 讨论(0)
提交回复
热议问题