Django URL mapping - NameError: name X is not defined

You have the NameError because you are referencing myapp in myproject/urls.py but haven't imported it.

The typical approach in Django is to use a string with include, which means that the import is not required.

url(r'^myapp/', include('myapp.urls')),

Since you have move the hello URL pattern into myapp/urls.py, you can remove from myapp.views import hello from myproject/urls.py.

Once you've made that change, you will get another NameError in myapp/urls.py. In this case, a common approach is to use a relative import for the app's views.

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^hello/$', views.hello),
]

Make sure you have imported following modules to urls.py.

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

in django 2.0 use these

from django.contrib import admin
from django.urls import path
from first_app import views

urlpatterns = [


    path('',views.index, name="index"),

    path('admin/', admin.site.urls),
]

Tags:

Django