Django URL mapping - NameError: name X is not defined -
[a similar question asked, not marked answered, here. considered continuing thread website told me i'm supposed post answer, seems have start new topic.] i'm trying follow this tutorial , i'm having problems url mapping. part described "so best practice create “url.py” per application , include in our main projects url.py file". relevant, hope, part of folder structure, arose following steps of tutorial letter (if possible; usage of 'patterns' module impossible example) , using django 1.10 following:
myproject/ myapp/ urls.py views.py myproject/ urls.py
the myproject/urls.py follows:
from django.conf.urls import include, url django.contrib import admin admin.autodiscover() myapp.views import hello urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^myapp/', include(myapp.urls)), ]
the myapp/urls.py follows:
from django.conf.urls import include, url urlpatterns = [ url(r'^hello/', myapp.views.hello), ]
the myapp/views.py follows:
from django.shortcuts import render def hello(request): return render(request, "hello.html", {})
however, running 'python manage.py runserver' results in following error:
url(r'^myapp/', include(myapp.urls)), nameerror: name 'myapp' not defined
installed_apps in settings.py contains 'myapp'.
i'd greatful tips on how deal nameerror! [or tips whatsoever might consider helpful!]
you have nameerror
because referencing myapp
in myproject/urls.py
haven't imported it.
the typical approach in django use string include, means import not required.
url(r'^myapp/', include('myapp.urls')),
since have move hello
url pattern myapp/urls.py
, can remove from myapp.views import hello
myproject/urls.py
.
once you've made change, nameerror
in myapp/urls.py
. in case, common approach use relative import app's views.
from django.conf.urls import url . import views urlpatterns = [ url(r'^hello/$', views.hello), ]
Comments
Post a Comment