...">

Django url template tag: the 'module' object has no 'views' attributes

The tag in question:

< a href="{% url django.contrib.auth.views.login %}">Login< /a> 

bindings:

 from django.contrib.auth import views <br /> ...<br /> (r'^login/$',views.login, {'redirect_field_name' : '/' }) <br />... 
+4
source share
3 answers

For some reason, he didn't like the way I imported it.

Decision:

 from django.contrib.auth.views import login (r'^login', login, etc.) 
+4
source

Named URLs are best used, they save a lot of maintenance work in the future and print first.

If you save the name of the URL the same way, you can rename the view function, move it to another application, etc. You will not 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') 
+5
source

I believe that I have something to contribute to this issue.

What was strange to me was that my code made sense in terms of how I used it, but it wouldn't work.

If in my urls I tried the following. Where helloworld is my django application name.

 import helloworld ... url(r'^test', helloworld.views.home1() , name='home'), 

Gives an error message. Although technically everything is correct. I imported my application, 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 looked at how they do their import in the URL section of their application. Go and look at this project on github. This is a great recommendation for a large-scale project. There is much to learn. https://github.com/django/djangoproject.com .

Here's how they import and configure URLs.

 from accounts import views as account_views ... url(r'^~(?P<username>[\w-]+)/$', account_views.user_profile, name='user_profile'), 

So, I changed my code to something similar

 import helloworld.views as helloView ... url(r'^test', helloView.home1 , name='home'), 

This is most likely something related to application / project / python spaces. I'm not quite sure. But my code works as expected, and I can still have different applications in their own namespaces. I just need to make sure that import app.view as somename is unique in the application / project / python namespace schema.

0
source

All Articles