如何通过url传递django中的变量? [英] How do I pass variables in django through the url?
问题描述
如何编码url字符串以考虑字符串中的特殊字符?
什么是正确的正则表达式我应该使用给定的字符串?
如何解码已编码的网址?
查看
author ='foo'
video ='bar123-456'
title ='Santorum:我不是一个有远见的人#在我的版本,它引用另一个变量所以语法错误不会发生。但是我把它留在这里是因为我想知道如何处理和。
related ='http://gdata.youtube.com/feeds/api/users/haha/uploads?v=2& max-results = 50'
url = urllib.quote('partner /'+ author +'/'+ video +'/'+ title +'/'+ related)
#我上面编码这个url字符串以考虑字符串中的特殊字符?
模板
< a href =/ {{url}}>< img src =img.png> / a>
urls.py
URL(R'^伙伴/(P< PARTNER_NAME>有[ - \w] +)/(P< VIDEO_ID> [ - \w] +)/(P< video_title> [ - \w] +)//(?P< related_feed>)/ $','video_player'),
#do我必须向正则表达式添加任何东西?
video_player function
def video_player (请求,作者,视频,相关):
#how我解码编码
的网址
编辑
我没有关系,作品,但仍然收到错误。
模板:
; href ={%url'reserve.views.video_player'author video title%}>
url:
?code> URL(R'^伙伴/(P<作者> [ - \w] +)/(P<视频> [ - \w] +)/(P<标题> [ - \w] +)/ $','video_player'),
我得到这个错误: / p>
NoReverseMatch at / partner / BuzzFeed /
反向为''video_player'与参数'('BuzzFeed','fXkqhhIlOtA','NY洋基:6基本
完整的urls.py
urlpatterns = patterns('reserve.views',
url(r'^ $','index'),
url(r'^ browse / $' ,'browse'),
url(r'^ faq / $','faq'),
url(r'^ about / $','about'),
url ''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& ,
url(r'^ user /(?P< user_id> [ - \w] +)/ $','view_reviews_user'),
#url(r'^ category /(?P< category_name> [ - \w] +)/ $','view_product_category'),
url(r'^ partner /(?P& [-\w] +)/ $','partner_channel'),
url(r'^ partner /(?P< author> [ - \w] +)/(?P< video& - '?'/(?P< video_title> [ - \w] +)/ $','video_player'),
url \w] +)/(?P< video> \w +)/(?P< title> \w +)/ $','video_player'),
url(r'^ admin /', include(admin.site.urls)),
)
将这些变量传递给模板,使用 url ,在发送到模板之前,只需执行此操作。
View.py
related = urllib.quote(related,safe ='')
模板
< 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&相关> \w +)/ $','video_player'),
strong>
如果你想去没有相关参数,或者如果有疑问,视频也可以是None,那么只需在你的视图中执行此操作:
def video_player(请求,作者,视频=无,相关=无):
现在您可以使用
< a href = {%url'path.to.video_player'author video%}> < img src =img.png> < / A>
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?
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
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.
full 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)),
)
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>
这篇关于如何通过url传递django中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!