【问题标题】:passing a get parameter in django named variable在 django 命名变量中传递 get 参数
【发布时间】:2013-07-23 19:37:25
【问题描述】:

我的 url 模式是在 django 中定义的 -

url(r'^import-contacts$', 'import_contacts', name='import_contacts'),

视图需要一个get参数,我在视图中解析并处理它,url的格式应该是-http://127.0.0.1:8000/import-contact/?service=google

在模板中我执行以下操作 -

<a href="{% url "import_contacts" %}"> Text Here </a>

这将生成以下 url - http://127.0.0.1:8000/import-contact/ 而不是 reqd 的 url。

关于如何使用命名的 url 参数传递获取变量的任何想法。

【问题讨论】:

  • Django 只会为你生成 URL,而不是 GET 参数 - 你必须自己明确地这样做

标签: django django-urls


【解决方案1】:

在标签后面加上即可:

<a href="{% url "import_contacts" %}?service=google"> 

【讨论】:

    【解决方案2】:

    你的网址格式应该是这样的:

    url(r'^import-contacts/?service=(?P<service>\w+)/$', 'import_contacts', name='import_contacts'),
    

    视图应该是这样的:

    def import_contacts(request, service):
        # Do something with get variable "service"
    

    现在,如果您只是使用

    <a href="{% url "import-contacts" %}">TEXT HERE</a>
    

    它会带你到 url http://127.0.0.1:8000/import-contacts/?service=

    您需要明确给出 GET 参数。硬编码或使用模板。

    【讨论】:

      猜你喜欢
      • 2015-12-22
      • 2021-11-21
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多