开发者

Problems with django url template tag, {% url %}

开发者 https://www.devze.com 2023-03-27 02:39 出处:网络
Hello I am trying to pass different urls from my site into a template. I thought it would be done by us开发者_C百科ing this in the template

Hello I am trying to pass different urls from my site into a template.

I thought it would be done by us开发者_C百科ing this in the template

{% url myappname.module.views.urlfunction %}

but that returns an error, saying that "Caught AttributeError while rendering: 'str' object has no attribute 'regex'"

I am not sure what that means. I followed this example url template tag in django template

urlpatterns = patterns('',
url(r'^$', 'myappname.module.views.start'),

and this is what I have entered in my template

{% url myappname.module.views.start %}

I have also tried this url pattern and template combo to no avial:

urlpatterns = patterns('',
url(r'^$', 'myappname.module.views.start', name="home"),

{% url "home" %}

What am I doing wrong?

Thanks.


In last example instead of

{% url "home" %}

try

{% url home %}

without quotes


add the following line in the top of the template:

{% load url from future %}

It will support the pattern

{% url "VIEW_NAME" args... %}


In all seriousness, I would consider not using the {% url %} scheme in django for simple cases. They are designed to provide some sort of flexibility that you will likely never take advantage of, and they are almost guaranteed to waste hours of your time trying to work out how to do it 'correctly'.

Just use action="/login/" or action ="/home/" and spend the hours you saved doing useful things, like writing code.

0

精彩评论

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