どのビューが呼び出されているかを理解できるように、追加情報をテンプレートに渡すこともできます。しかし、それは私が推測するエレガントな解決策ではありません。
もちろんそうだ。次に例を示します。
def view1(request, form_class=MyForm, template_name='myapp/page.html'):
# app code here
this_url = reverse('view1')
render_to_response(template_name, locals(), RequestContext(request))
def view2(request, form_class=MyForm, template_name='myapp/page.html'):
# app code here
this_url = reverse('view2')
render_to_response(template_name, locals(), RequestContext(request))
myapp/page.html
:
<a href="{{ this_url }}">Webpage</a>
dynurl
たとえば、最初の引数をビュー名ではなく変数として受け取る独自の url タグを作成することもできます。
def view2(request, form_class=MyForm, template_name='myapp/page2.html'):
# app code here
this_view = 'view2'
render_to_response(template_name, locals(), RequestContext(request))
myapp/page.html
:
{% load dynurl_tags %}
<a href="{% dynurl this_view %}">Webpage</a>
ただし、現在のビューへのリンクが必要な理由を正確に説明していません。同じページにリンクするためですか?これを行うには、いくつかの方法があります。
<a href="">technically this points back to the same page</a>
<a href="{{ request.path }}">this url is the full path before the query string</a>
<a href="{{ request.get_full_path }}">this url is the full path plus the query string</a>
2 つのビューの主な違いは何かを考え、それらの違いを説明する変数を考え出すと役立つと思います。次に、テンプレートでその変数を使用して、新しい URL を決定します。
より複雑な問題については、Pinax グループと、それらがタグを実装する{% groupurl %}
方法を確認することをお勧めします。基本的に、特定のアプリのすべての URL を複製し、URL の特別なグループベースの逆引き参照を作成するために使用される「グループ」変数を渡すことができます。