2016-04-28 28 views
4

私は、ページのリストに基づいて、動的なURLリストを作成しようとしています。私urls.pyで変数djangoテンプレートを使用した動的URL

、アプリケーション全体では、名前空間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を使用することができます
+0

のhrefなどから "" 削除しようとする:.. Dovi

答えて

7

{% url "base:"|add:section pk=project.id %} 
関連する問題