简体   繁体   中英

How to pass context objects as Django url template tag arguments

In my django project, I have two model classes - Category & Action. Category is the ForeignKey of Action (all Actions fall under a Category).

Within my django templates, I'd like to be able to pass a context object into a {% url %} tag that accepts a string containing the name of a Category as an argument. So in a perfect world my django template would have a line like:

<form action="{% url 'actions' {{ category }} %}">

And when submitted, Django would follow the form to a urlpattern like:

url(r'^(?P<category_name>\\w+)', views.actions, name='actions'),

But unfortunately it doesn't look like I'm able to nest template filters into template tags. What's an alternate way I can reference the ForeignKey of a Django object within a url tag? Or perhaps a different way to structure my code?

上下文变量可直接在url templatetag中使用

<form action="{% url 'actions' category_name=category %}">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM