Django模板:如何将命名URL和字符串连接起来? [英] Django templates: how to concatenate a named URL and a string?

查看:38
本文介绍了Django模板:如何将命名URL和字符串连接起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:

项目应用程序的 urls.py :

urlpatterns = [
    url(r'^main/', include('main.urls', namespace='main')),
]

应用的 urls.py :

urlpatterns = [
    url(r'^add/$', views.add, name='add'),
]

我试图在模板中以/main/add/whatever/创建一个URL,假设如下:

I am trying to create an URL as /main/add/whatever/ in a template, hypothetically as follows:

<form method="POST" action="{% url 'main:add' %}" + "whatever/">

我该怎么做?

推荐答案

只需使用:

<form method="POST" action="{% url 'main:add' %}whatever/">

花括号中的代码将由您的模板系统呈现,并且将出现在任何之前,从而生成以下HTML:

The code inside the curly braces will be rendered by your template system, and will precede the whatever, thus generating the following HTML:

<form method="POST" action="/main/add/whatever/">

这篇关于Django模板:如何将命名URL和字符串连接起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆