如何通过url传递django中的变量?

sha*_*aka 6 python django url templates view

我试图传递一些变量,但我遇到了一些麻烦,特别是有3个问题.如何对url字符串进行编码以考虑字符串中的特殊字符?鉴于字符串,我应该使用的正确正则表达式是什么?我如何解码已编码的网址?

视图

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?
Run Code Online (Sandbox Code Playgroud)

模板

<a href="/{{url}}" > <img src="img.png" > </a>
Run Code Online (Sandbox Code Playgroud)

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?
Run Code Online (Sandbox Code Playgroud)

video_player功能

def video_player(request, author, video, related):
    #how do I decode the urls that are encoded
Run Code Online (Sandbox Code Playgroud)

编辑

我尝试了它没有相关,看看它是否有效,但仍然出现错误.

模板:

<a href="{% url 'reserve.views.video_player' author video title   %}" >
Run Code Online (Sandbox Code Playgroud)

网址:

url(r'^partner/(?P<author>[-\w]+)/(?P<video>[-\w]+)/(?P<title>[-\w]+)/$', 'video_player'),
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

NoReverseMatch at/partner/BuzzFeed/Reverse for''video_player''with arguments'('BuzzFeed','fXkqhhIlOtA','NY Yankees:6 Essential Pieces of Postseason Memorabilia')'和关键字参数'{}'找不到.

完整的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)),
)
Run Code Online (Sandbox Code Playgroud)

Ahs*_*san 16

将这些变量原样传递给模板,使用url,然后发送到模板,只需在视图中执行此操作.

View.py

related = urllib.quote(related, safe='')
Run Code Online (Sandbox Code Playgroud)

模板

<a href="{% url 'path.to.video_player' author video related %}" > <img src="img.png" > </a>
Run Code Online (Sandbox Code Playgroud)

Url.py

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

编辑

如果您想要没有相关参数,或者如果有疑问,视频也可以是无,那么只需在您的视图中执行此操作:

def video_player(request, author, video=None, related=None):
Run Code Online (Sandbox Code Playgroud)

现在你可以使用网址了

<a href="{% url 'path.to.video_player' author video %}" > <img src="img.png" > </a>
Run Code Online (Sandbox Code Playgroud)