【发布时间】:2012-09-30 12:47:39
【问题描述】:
我正在尝试传递一些变量,但我遇到了一些麻烦,特别是有 3 个问题。 如何编码 url 字符串以考虑字符串中的特殊字符? 给定字符串,我应该使用什么正确的正则表达式? 以及如何解码已经编码的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?
模板
<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函数
def video_player(request, author, video, related):
#how do I decode the urls that are encoded
编辑
我尝试了没有相关的,看看它是否有效,但仍然出现错误。
模板:
<a href="{% url 'reserve.views.video_player' author video title %}" >
网址:
url(r'^partner/(?P<author>[-\w]+)/(?P<video>[-\w]+)/(?P<title>[-\w]+)/$', 'video_player'),
我收到此错误:
NoReverseMatch 在 /partner/BuzzFeed/ 使用参数 '('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)),
)
【问题讨论】:
-
title = 'Santorum: "I'm Not a Visionary"'语法无效! -
如何获取注册网址?
-
你的 urls.py 中只有这一行吗?您是否将此 url 模式添加到其他 urlpatterns 中?
-
否;我在上面的编辑中添加了完整的 urls.py
-
我尝试删除模板中的reserve.views,但还是同样的错误。
标签: python django templates url view