问题
I made this many times and it worked, but not this time.
I get this error when I try to use {% url path.to.view %} django's template tage:
AttributeError at /login/ 'str' object has no attribute 'regex'
urls.py (main)
urlpatterns= patterns('', (r'', include('authenticate.urls')), )
urls.py (my app)
urlpatterns= patterns('authenticate.views', url(r'^login/$','login'),)
login.html
{{ form }}
{% url authenticate.views.login %} < --- Error comes here
in the views:
return render_to_response('login.html',{'form':form},context_instance=RequestContext(request), )
Doesn't also work with:
{% url authenticate.views.login %}
{% url 'authenticate.views.login' %}
{% url "authenticate.views.login" %}
This is on django 1.4; what possibly I'm doing wrong, or what do I miss in that version of django?
Thanks in Advance!
Update:
I can also add that using reverse in my views doesn't work and gives me the same error above:
from django.core.urlresolvers import reverse
result = reverse('django.contrib.auth.views.password_reset')
HttpResponse(result)
Error:
AttributeError at /abc/ 'str' object has no attribute 'regex'
回答1:
Check this https://docs.djangoproject.com/en/1.4/topics/http/urls/
You can add a name to your URL to help the refer. Also check how you are declaring the pattern. It should be done like this:
urlpatterns = patterns('',
url(r'^archive/(\d{4})/$', archive, name="full-archive"),
url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}, "arch-summary"),
)
回答2:
I got it :))
The problem wasn't in the url template tag, it was in another urls file that's been included in the main one; the problem doesn't show till my app. hits url template tage or django's reverse method, otherwise, django doesn't complain about any url!!
来源:https://stackoverflow.com/questions/13473949/django-url-template-tag-url-error-on-django-1-4