带有可变 django 模板的动态 URL
Dynamic URL with variable django template
我正在尝试基于页面列表构建一个动态的 url 列表。
在我的 urls.py 中,整个应用程序都在命名空间 base
后面:
urlpatterns = patterns('',
url(r'^(?P<pk>[\w]+)/title/$', TitleSection.as_view(), name='title'),
url(r'^(?P<pk>[\w]+)/amount/$', AmountSection.as_view(), name='amount'),
url(r'^(?P<pk>[\w]+)/description/$', DescriptionSection.as_view(), name='description'), )
在我的 context
数据中,我有以下列表:
sections: ['title', 'amount', 'description']
我正在尝试为部分中的每个元素构建 url。
我尝试了以下方法:
{% for section in sections %}
<a href="{% url "base:"+section pk=object.id %}">..</a>
{% endfor %}
但是我得到以下错误:
Could not parse the remainder: '+section' from '"base:"+section'
然后我尝试了:
<a href="{% url "base:{{section}}" pk=project.id %}">{{ section }}</a>
错误:
Reverse for '{{section}}' with arguments '()' and keyword arguments '{u'pk': 77}' not found. 0 pattern(s) tried: []
你知道怎么做吗?
您可以使用 add template filter:
{% url "base:"|add:section pk=project.id %}
我的案例也有效
{% for section in sections %}
<a href="{% url "base" pk=object.id %}">..</a>
{% endfor %}
其中 url 模式是
url=[
path('base/<pk>/',base,name='base'),
]
我的每个模型都有一个列表视图、一个 create/update 视图和一个删除视图。这些将由客户组织内的不同功能使用,以维护他们负责的数据。每个列表视图都有指向相关创建、更新和删除视图的链接。我想构建一个页面,其中包含指向列表视图的链接列表。这是我的做法。
我在 views.py 中创建了一个基于函数的视图。
def index(request):
app = request.resolver_match.app_name
models = apps.get_app_config(app).get_models()
names = [model._meta.model.__name__ for model in models]
context = {
"names" : names,
}
return render(request, app + '/index.html', context)
我创建了一个模板 app/templates/app/index。html
{% for name in names %}
<li><a href="{% url request.resolver_match.app_name|add:':'|add:name|lower|add:'-review'%}">{{ name}}</a></li>
{% endfor %}
我正在尝试基于页面列表构建一个动态的 url 列表。
在我的 urls.py 中,整个应用程序都在命名空间 base
后面:
urlpatterns = patterns('',
url(r'^(?P<pk>[\w]+)/title/$', TitleSection.as_view(), name='title'),
url(r'^(?P<pk>[\w]+)/amount/$', AmountSection.as_view(), name='amount'),
url(r'^(?P<pk>[\w]+)/description/$', DescriptionSection.as_view(), name='description'), )
在我的 context
数据中,我有以下列表:
sections: ['title', 'amount', 'description']
我正在尝试为部分中的每个元素构建 url。
我尝试了以下方法:
{% for section in sections %}
<a href="{% url "base:"+section pk=object.id %}">..</a>
{% endfor %}
但是我得到以下错误:
Could not parse the remainder: '+section' from '"base:"+section'
然后我尝试了:
<a href="{% url "base:{{section}}" pk=project.id %}">{{ section }}</a>
错误:
Reverse for '{{section}}' with arguments '()' and keyword arguments '{u'pk': 77}' not found. 0 pattern(s) tried: []
你知道怎么做吗?
您可以使用 add template filter:
{% url "base:"|add:section pk=project.id %}
我的案例也有效
{% for section in sections %}
<a href="{% url "base" pk=object.id %}">..</a>
{% endfor %}
其中 url 模式是
url=[
path('base/<pk>/',base,name='base'),
]
我的每个模型都有一个列表视图、一个 create/update 视图和一个删除视图。这些将由客户组织内的不同功能使用,以维护他们负责的数据。每个列表视图都有指向相关创建、更新和删除视图的链接。我想构建一个页面,其中包含指向列表视图的链接列表。这是我的做法。
我在 views.py 中创建了一个基于函数的视图。
def index(request):
app = request.resolver_match.app_name
models = apps.get_app_config(app).get_models()
names = [model._meta.model.__name__ for model in models]
context = {
"names" : names,
}
return render(request, app + '/index.html', context)
我创建了一个模板 app/templates/app/index。html
{% for name in names %}
<li><a href="{% url request.resolver_match.app_name|add:':'|add:name|lower|add:'-review'%}">{{ name}}</a></li>
{% endfor %}