I have a view that looks like this:
我有一个看起来像这样的观点:
def selectCity(request, the_city):
request.session["ciudad"] = the_city
city = request.session["ciudad"]
return HttpResponse('Ciudad has been set' + ": " + city)
And a URL that looks like this:
一个看起来像这样的URL:
url(r'^set/$', views.selectCity, {'the_city': 'gye'}, name='ciudad'),
Now when I visit /set/ I get the appropriate response with the session variable set from the value on the dict in the url {'the_city': 'gye'}
现在当我访问/设置/我从url {'the_city':'gye'}中dict的值设置会话变量时获得适当的响应
Now, what I would like to do is modify my program so that I can call the 'ciudad' url from a different template (index.html) and set the appropriate session variable. So I would call it using reverse URL matching with an additional argument doing something like this:
现在,我想要做的是修改我的程序,以便我可以从另一个模板(index.html)调用'ciudad'url并设置适当的会话变量。所以我会使用反向URL匹配调用它,并使用另外一个参数执行以下操作:
<div class="modal-body">
<a tabindex="-1" href="{% url ciudad 'the_city':'uio' %}">Quito</a>
<br/>
<a tabindex="-1" href="{% url ciudad 'the_city':'gye' %}">Guayaquil</a>
</div>
I have tried to modify the url and the views and the reverse url call in various ways to try to get this to work but, I can't seem to figure it out. I would really appreciate some pointers.
我试图以各种方式修改url和视图以及反向url调用以尝试使其工作但是,我似乎无法弄明白。我真的很感激一些指示。
2 个解决方案
#1
10
You can pass the relevant arguments in url tag, if your url (in urls.py
) has any capturing group.
如果您的url(在urls.py中)有任何捕获组,您可以在url标记中传递相关参数。
url(r'^set/(?P<the_city>\w+)/$', views.selectCity, {'the_city': 'gye'}, name='ciudad'),
Then in template:
然后在模板中:
<a tabindex="-1" href="{% url ciudad the_city='gye' %}">Guayaquil</a>
#2
1
Check out captured parameters. Something like this might work:
检查捕获的参数。这样的事情可能有用:
url(r'^set/$', views.selectCity, {'the_city': 'gye'}, name='ciudad'),
url(r'^set/(?P<the_city>\w+)/$', views.selectCity, name='ciudad'),
#1
10
You can pass the relevant arguments in url tag, if your url (in urls.py
) has any capturing group.
如果您的url(在urls.py中)有任何捕获组,您可以在url标记中传递相关参数。
url(r'^set/(?P<the_city>\w+)/$', views.selectCity, {'the_city': 'gye'}, name='ciudad'),
Then in template:
然后在模板中:
<a tabindex="-1" href="{% url ciudad the_city='gye' %}">Guayaquil</a>
#2
1
Check out captured parameters. Something like this might work:
检查捕获的参数。这样的事情可能有用:
url(r'^set/$', views.selectCity, {'the_city': 'gye'}, name='ciudad'),
url(r'^set/(?P<the_city>\w+)/$', views.selectCity, name='ciudad'),