SilentDev
SilentDev

Reputation: 22747

Urls.py says my view is not defined (class based view)

This is my urls.py for my project:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^CMS/', include('CMSApp.urls')),
    url(r'^admin/', include(admin.site.urls)),
]

and this is my urls.py for my app (CMSApp):

from django.conf.urls import patterns, url

urlpatterns = patterns(
        'CMSApp.views',
        url(r'^users$', user_list.as_view()),
        url(r'^users/(?P<pk>[0-9]+)$', user_detail.as_view()),
)

When I go to

CMS/users

it gives me a name error saying that

name 'user_list' is not defined

Any idea why?

When I do

from CMSApp import views

urlpatterns = patterns(
        '',
        url(r'^users$', views.user_list.as_view()),
)

it works but I'm just wondering why the former does not work?

Upvotes: 0

Views: 1518

Answers (2)

Joey Wilhelm
Joey Wilhelm

Reputation: 5819

It appears that you're using Django 1.8 for your project; the behavior you're trying to use was removed in 1.8, as documented here: https://docs.djangoproject.com/en/1.8/releases/1.8/#django-conf-urls-patterns

Upvotes: 1

rafaelc
rafaelc

Reputation: 59274

You have to import

from CMSApp.views import user_list

Otherwise django won't know user_list is defined.

If you just use user_list without importing it explictly, python will consider it is a local variable and return NameError. Once user_list is defined in views.py, you have to explicitly tell python to search for it there.

Upvotes: 1

Related Questions