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/">