开发者

How to call controller function from template in Django?

开发者 https://www.devze.com 2023-01-17 11:17 出处:网络
How to call controller funct开发者_开发问答ion from template in Django instead of hardcoding URIs?I guess You mean \"link to it\".

How to call controller funct开发者_开发问答ion from template in Django instead of hardcoding URIs?


I guess You mean "link to it".

This can be done via {% url %} tag, see docs

If Your intent is to call view function directly, this is not possible by design - templates should not know about views, it's a separate layer.


How to call controller function from template

Not sure what you mean here. If you mean to provide an an HTML link to it see the second part of my answer below.

instead of hardcoding URIs?

In case you haven't already, use the {% url %} tag. Use this in conjunction with named URLs in your url config.


If you mean adding URL to the resulting HTML, then either use {% url %} tag as others, or get used to writing and using model_instance.get_absolute_url(). Personally I'm using the latter whenever possible, adding also custom "URL functions" like get_delete_url() etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消