The tag in question:
< a href="{% url django.contrib.auth.views.login %}">Login< /a>
URLConf:
from django.contrib.auth import views <br />
...<br />
(r开发者_JAVA技巧'^login/$',views.login, {'redirect_field_name' : '/' })
<br />...
it's better to use named urls, they save a lot of maintenance work in the future and typing in the first place.
if you keep name of the url the same, you can rename view function, move it to a different app, etc. you won't need to change templates or other places using this url at all.
in urls.py:
url(r'^login/',path.to.view,name='login',...)
in templates:
<a href="{%url login%}">login here</a>
in views:
login_url = reverse('login')
For some reason it didn't like the way I was importing it.
Solution:
from django.contrib.auth.views import login
(r'^login', login, etc.)
I believe I have some thing to contribute to this question.
What was strange for me is that my code made sense in terms of how I was using it but it would not work.
If in my urls I tried the following. Where helloworld is my django app name.
import helloworld
...
url(r'^test', helloworld.views.home1() , name='home'),
It produces an error. Even though technically every thing is correct. I have imported my app which is a python module automatically created by django manage startapp
module' object has no attribute 'views'
I found the source code for the django project site on github and had a look at how they do their imports in the urls section of their app. Go and have a look at this project on github. It is an excellent reference for a large scale project implementation. There is a lot to learn there. https://github.com/django/djangoproject.com.
This is how they do their imports and url configuration.
from accounts import views as account_views
...
url(r'^~(?P<username>[\w-]+)/$', account_views.user_profile, name='user_profile'),
So I modified my code to something similar
import helloworld.views as helloView
...
url(r'^test', helloView.home1 , name='home'),
This is most likely some thing to do with the app / project / python namespaces. I am not entirely sure. But my code works as expected and I can still have my different apps in their own respective namespaces. I just need to make sure that the import app.view as somename
is unique in the app / project / python namespace scheme.
精彩评论