是否可以将包含反向名称的参数传递给 Django 模板 URL 标记
例如:
note: In this example
条件网址is not a reverse name.
网址.py:
urlpatterns = patterns('views',
url(r'^iframeViewURL/$', 'iframeView', name='iframeView'),
url(r'^url_a/', 'view_a', name='view_a'),
url(r'^url_b/', 'view_b', name='view_b'),
)
视图.py:
def iframeView(**kwargs):
kw = kwargs
if kw['condition']:
conditional_url = 'view_a' # name of the URL pattern
else:
conditional_url = 'view_b' # name of the URL pattern
return render_to_response('iframe.html', {'conditional_URL': conditional_url},
context_instance=RequestContext(request, {}))
def view_a(*args):
pass
def view_b(*args):
pass
iframe.html:
<iframe src="{% url conditional_URL *args %}">
</iframe>
我尝试了这个,但它不起作用,因为 conditional_URL 不是任何 url 模式的名称。