1
from django.conf.urls import url, patterns, include
from django.contrib import admin
from django.views.generic import TemplateView
from collection import *


#from collection.views import index,thing_detail,edit_thing

urlpatterns = [ 
        url(r'^$', views.index, name='home'),
        url(r'^about/$',TemplateView.as_view(template_name='about.html'),name='about'),
        url(r'^contact/$',TemplateView.as_view(template_name='contact.html'),name='contact'),
        url(r'^things/(?P<slug>[-\w]+)/$', 'views.thing_detail' ,name='thing_detail'),
        url(r'^things/(?P<slug>[-\w]+)/edit/$', 'views.edit_thing',name='edit_thing'), 
        url(r'^admin/', include(admin.site.urls)),
]  

After running the server there is an error "NameError: name 'views' is not defined"

Any help ??

Hat hout
  • 471
  • 1
  • 9
  • 18
  • you aren't importing your own views – ritlew Jul 15 '16 at 20:33
  • For what specific `view` you've got `NameError`, and If may I suggest change `from collection import *` to `from collection import views` if `collection` is your `app` name – copser Jul 15 '16 at 20:34
  • First Import your views explicitly. Also avoid using string in urls (`'views.edit_thing'`) as it would raise a Deprecation Warning and is not a good habit too. And lastly use the callable view itself `edit_thing` rather `views.edit_thing`. – kapilsdv Jul 16 '16 at 05:44

3 Answers3

2

You aren't importing your own views.

Try adding this to your urls.py:

from . import views

Or if you are importing them from a specific app, try replacing . with the app name

ritlew
  • 1,622
  • 11
  • 13
  • This will probably solve the problem, but relative imports are not a good idea. http://stackoverflow.com/questions/4209641/absolute-vs-explicit-relative-import-of-python-module – Gocht Jul 15 '16 at 20:35
  • With how urls work in Django, I feel like relative imports won't confuse anyone – ritlew Jul 15 '16 at 20:38
  • `Python 2.1.2` pragmatic is better than dogmatic – ritlew Jul 15 '16 at 20:41
0

First thing I notice is the import *, realize that this will/can cause confusion for other Developers reading your scripts. Python has a methodology that insists that explicit is better than implicit. Which in this senario means you should be explicit about what you are importing.

from django.conf.urls import url, patterns, include
from django.contrib import admin
from django.views.generic import TemplateView
from collection import views as collection_views

urlpatterns = [ 
        # Function Based Views
        url(r'^$', collection_views.index, name='home'),
        url(r'^things/(?P<slug>[-\w]+)/$', collection_views.thing_detail ,name='thing_detail'),
        url(r'^things/(?P<slug>[-\w]+)/edit/$', collection_views.edit_thing,name='edit_thing'), 
        # Class Based Views            
        url(r'^about/$',TemplateView.as_view(template_name='about.html'),name='about'),
        url(r'^contact/$',TemplateView.as_view(template_name='contact.html'),name='contact'),
        # Admin
        url(r'^admin/', include(admin.site.urls)),
]  

Here instead of importing everything from collection I'm importing just your views and assigning them to a variable. Then using that variable in the URL definitions.

marcusshep
  • 1,916
  • 2
  • 18
  • 31
-1

Be sure to import your views by specifying its location and the methods inside the view to be imported on your urls.py.

from . collection import *

(line above means from current location find collection.py and import everything on it)

Happy coding!

Abz Rockers
  • 338
  • 1
  • 11