Python 如何通过 url 在 django 中传递变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12812716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pass variables in django through the url?
提问by sharataka
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 characters in the string? What is the correct regex I should use given the strings? And how do I decode the urls that have been encoded?
我试图传递一些变量,但我遇到了一些麻烦,特别是有 3 个问题。如何编码 url 字符串以考虑字符串中的特殊字符?给定字符串,我应该使用什么正确的正则表达式?以及如何解码已编码的网址?
view
看法
author = 'foo'
video = 'bar123-456'
title = 'Santorum: "I'm Not a Visionary"' # in my version, it is referencing another variable so the syntax error doesn't occur. But I left it in here because I want to know how to deal with " and '.
related = 'http://gdata.youtube.com/feeds/api/users/haha/uploads?v=2&max-results=50'
url = urllib.quote('partner/' + author+ '/'+ video+'/'+ title + '/' + related)
#How do I encode this url string above to take into account the special characters in the string?
template
模板
<a href="/{{url}}" > <img src="img.png" > </a>
urls.py
网址.py
url(r'^partner/(?P<partner_name>[-\w]+)/(?P<video_id>[-\w]+)/(?P<video_title>[-\w]+)//(?P<related_feed>)/$', 'video_player'),
#do I have to add anything to the regex?
video_player function
视频播放器功能
def video_player(request, author, video, related):
#how do I decode the urls that are encoded
edit
编辑
I tried it without related to see if it works but am still getting an error.
我在没有相关的情况下尝试了它,看看它是否有效,但仍然出现错误。
template:
模板:
<a href="{% url 'reserve.views.video_player' author video title %}" >
url:
网址:
url(r'^partner/(?P<author>[-\w]+)/(?P<video>[-\w]+)/(?P<title>[-\w]+)/$', 'video_player'),
I get this error:
我收到此错误:
NoReverseMatch at /partner/BuzzFeed/ Reverse for ''video_player'' with arguments '('BuzzFeed', 'fXkqhhIlOtA', 'NY Yankees: 6 Essential Pieces of Postseason Memorabilia')' and keyword arguments '{}' not found.
NoReverseMatch at /partner/BuzzFeed/ 反向“video_player”,参数为“('BuzzFeed', 'fXkqhhIlOtA', 'NY Yankees: 6 Essential Pieces of Postseason Memorabilia')' 和关键字参数 '{}' 未找到。
full urls.py
完整的 urls.py
urlpatterns = patterns('reserve.views',
url(r'^$', 'index'),
url(r'^browse/$', 'browse'),
url(r'^faq/$', 'faq'),
url(r'^about/$', 'about'),
url(r'^contactinfo/$', 'contactinfo'),
url(r'^search/$', 'search'),
(r'^accounts/', include('registration.backends.default.urls')),
(r'^accounts/profile/$', 'profile'),
(r'^accounts/create_profile/$', 'user_profile'),
(r'^accounts/edit_profile/$', 'edit_profile'),
url(r'^products/(?P<product_name>[-\w]+)/reviews/$', 'view_reviews'),
url(r'^products/(?P<product_id>\d+)/reviews/$', 'view_reviews'),
url(r'^user/(?P<user_id>[-\w]+)/$', 'view_reviews_user'),
#url(r'^category/(?P<category_name>[-\w]+)/$', 'view_product_category'),
url(r'^partner/(?P<partner_name>[-\w]+)/$', 'partner_channel'),
url(r'^partner/(?P<author>[-\w]+)/(?P<video>[-\w]+)/(?P<video_title>[-\w]+)/$', 'video_player'),
url(r'^partner/(?P<author>[-\w]+)/(?P<video>\w+)/(?P<title>\w+)/$', 'video_player'),
url(r'^admin/', include(admin.site.urls)),
)
采纳答案by Ahsan
Pass these variables as it is to template, there use url, before sending to template just do this in view.
将这些变量按原样传递给模板,使用url,在发送到模板之前只需在视图中执行此操作。
View.py
查看.py
related = urllib.quote(related, safe='')
template
模板
<a href="{% url 'path.to.video_player' author video related %}" > <img src="img.png" > </a>
Url.py
网址.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:
如果您想不使用相关参数,或者有疑问 video 也可以为 None ,那么只需在您的视图中执行此操作:
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>
回答by Alireza
in newer versions of python you could simply just type: Example:
在较新版本的python中,您只需键入:例如:
path('<int:id>/delete/', delete_view, name = 'delete'),
path('<int:id>/delete/', delete_view, name = 'delete'),

