Django url 获取带参数的请求
Django url get request with parameter
我有以下 HTML 文件
<!DOCTYPE html>
<body>
{% for champion in champions %}
<a href="{% url 'guide_form' %}{{champion}}">{{champion}}</a>
{% endfor %}
</body>
</html>
这些是我的 URLS
urlpatterns = patterns('', url(r'^select_champion/$', views.select_champion, name='select_champion'),
url(r'^guide_form/(?P<champion>\w+)/$', views.guide_form, name='guide_form'),
url(r'^create_guide/$', views.create_guide, name='create_guide'),
url(r'^get_guide/(?P<id>\d+)/$', views.get_guide, name='get_guide'),
url(r'^guide_list/(?P<champion>\w+)/$', views.get_guide_list, name='get_guide_list'),
)
当我尝试 select 冠军时,出现以下错误:
Reverse for 'guide_form' with arguments '()' and keyword arguments
'{}' not found. 1 pattern(s) tried:
['guides/guide_form/(?P\w+)/$']
当我改变这一行时
<a href="{% url 'guide_form' %}{{champion}}">{{champion}}</a>
对此
<a href="{% url 'create_guide' %}{{champion}}">{{champion}}</a>
我没有收到错误,但当然调用了错误的 URL。我想要 select 一名冠军,并希望根据 url 交付冠军,以便可以在指南表格中写一篇关于他的指南。
您对如何解决我的问题有什么建议吗?
应该是这样的
<!DOCTYPE html>
<body>
{% for champion in champions %}
<a href="{% url 'guide_form' champion %} ">{{champion}}</a>
{% endfor %}
</body>
</html>
差不多了,你需要:
<a href="{% url 'guide_form' champion=champion %}">{{ champion }}</a>
您的 url 模式有一个关键字参数 champion
,值是模板变量 {{ champion }}
。 url 标签理解模板上下文,所以你不需要 {{ }}
围绕变量;而是直接将其作为参数传递给 url 标记。
我有以下 HTML 文件
<!DOCTYPE html>
<body>
{% for champion in champions %}
<a href="{% url 'guide_form' %}{{champion}}">{{champion}}</a>
{% endfor %}
</body>
</html>
这些是我的 URLS
urlpatterns = patterns('', url(r'^select_champion/$', views.select_champion, name='select_champion'),
url(r'^guide_form/(?P<champion>\w+)/$', views.guide_form, name='guide_form'),
url(r'^create_guide/$', views.create_guide, name='create_guide'),
url(r'^get_guide/(?P<id>\d+)/$', views.get_guide, name='get_guide'),
url(r'^guide_list/(?P<champion>\w+)/$', views.get_guide_list, name='get_guide_list'),
)
当我尝试 select 冠军时,出现以下错误:
Reverse for 'guide_form' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['guides/guide_form/(?P\w+)/$']
当我改变这一行时
<a href="{% url 'guide_form' %}{{champion}}">{{champion}}</a>
对此
<a href="{% url 'create_guide' %}{{champion}}">{{champion}}</a>
我没有收到错误,但当然调用了错误的 URL。我想要 select 一名冠军,并希望根据 url 交付冠军,以便可以在指南表格中写一篇关于他的指南。 您对如何解决我的问题有什么建议吗?
应该是这样的
<!DOCTYPE html>
<body>
{% for champion in champions %}
<a href="{% url 'guide_form' champion %} ">{{champion}}</a>
{% endfor %}
</body>
</html>
差不多了,你需要:
<a href="{% url 'guide_form' champion=champion %}">{{ champion }}</a>
您的 url 模式有一个关键字参数 champion
,值是模板变量 {{ champion }}
。 url 标签理解模板上下文,所以你不需要 {{ }}
围绕变量;而是直接将其作为参数传递给 url 标记。