9

いくつかの変数を渡そうとしていますが、問題があり、具体的には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:

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

このエラーが発生します:

引数'(' BuzzFeed'、' fXkqhhIlOtA'、' NY Yankees:6 Essential Pieces of Postseason Memorabilia')'およびキーワード引数'{}'を使用した''video_player''の/partner/ BuzzFeed/ReverseでのNoReverseMatchが見つかりません。

完全な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)),
)
4

2 に答える 2

17

これらの変数をそのままテンプレートに渡します。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<related>\w+)/$', 'video_player'),

編集

関連するパラメータなしで行きたい場合、またはビデオもNoneになり得る疑いがある場合は、ビューでこれを実行してください。

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

これで、次のURLを使用できます。

<a href="{% url 'path.to.video_player' author video %}" > <img src="img.png" > </a>
于 2012-10-10T06:08:18.437 に答える
4

新しいバージョンのPythonでは、次のように入力するだけです。例:

path('<int:id>/delete/', delete_view, name = 'delete'),
于 2020-03-19T10:40:42.147 に答える