加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Django url参数和反向URL

发布时间:2020-12-20 12:21:25 所属栏目:Python 来源:网络整理
导读:我有一个看起来像这样的观点: def selectCity(request,the_city): request.session["ciudad"] = the_city city = request.session["ciudad"] return HttpResponse('Ciudad has been set' + ": " + city) 一个看起来像这样的URL: url(r'^set/$',views.selec
我有一个看起来像这样的观点:

def selectCity(request,the_city):
    request.session["ciudad"] = the_city
    city = request.session["ciudad"]
    return HttpResponse('Ciudad has been set' + ": " + city)

一个看起来像这样的URL:

url(r'^set/$',views.selectCity,{'the_city': 'gye'},name='ciudad'),

现在当我访问/设置/我从url {‘the_city’:’gye’}中dict上的值设置会话变量时得到相应的响应

现在,我想要做的是修改我的程序,以便我可以从不同的模板(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>

我试图以各种方式修改url和视图以及反向url调用以尝试使其工作但是,我似乎无法弄明白.
我真的很感激一些指示.

解决方法

如果您的url(在urls.py中)有任何捕获组,您可以在url标记中传递相关参数.

url(r'^set/(?P<the_city>w+)/$',

然后在模板中:

<a tabindex="-1" href="{% url ciudad the_city='gye' %}">Guayaquil</a>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读