'Django URL mapping - NameError: name X is not defined

[A similar question was asked, but not marked as answered, here. I considered continuing that thread but the website told me I'm only supposed to post an answer, so it seems I have to start a new topic.] I'm trying to follow this tutorial and I'm having problems with the URL mapping. Specifically with the part described as "So best practice is to create an “url.py” per application and to include it in our main projects url.py file". The relevant, I hope, part of the folder structure, which arose by following steps of the tutorial to the letter (if possible; usage of the 'patterns' module was impossible for example) and using Django 1.10 is the following:

myproject/
  myapp/
    urls.py
    views.py
  myproject/
    urls.py

The myproject/urls.py is as follows:

from django.conf.urls import include, url

from django.contrib import admin
admin.autodiscover()

from myapp.views import hello

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

The myapp/urls.py is as follows:

from django.conf.urls import include, url

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

The myapp/views.py is as follows:

from django.shortcuts import render

def hello(request):
   return render(request, "hello.html", {})

However, running 'python manage.py runserver' results in the following error:

url(r'^myapp/', include(myapp.urls)),
NameError: name 'myapp' is not defined

INSTALLED_APPS in settings.py contains 'myapp'.

I'd be greatful for any tips on how to deal with the NameError! [Or any tips whatsoever that anyone might consider to be helpful!]



Solution 1:[1]

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

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

Solution 2:[2]

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),
]

Solution 3:[3]

your app URL has to be a string so, here is how the code should look like.

from django.conf.urls import include, url

from django.contrib import admin admin.autodiscover()

from myapp.views import hello

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

also, note that from python 2 upward the regular expression is not needed. change URL to path from django.conf.URLs import include path

from Django.contrib import admin admin.autodiscover()

from myapp.views import hello

urlpatterns = [
path('^admin/', include(admin.site.urls)),
path('^myapp/', include('myapp.urls')),
 ]

Solution 4:[4]

In Django 2.1.7 here is the default urls .py file

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

urlpatterns = [
    path('admin/', admin.site.urls),
]

so we need to add this line as well from django.conf.urls import url

Solution 5:[5]

I have followed @Alasdair answers

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.

Unfortunately, it didn't work out(I still got the name X is not defined error). Here is how I do it.

from django.contrib import admin
from django.urls import include
from django.conf.urls import url
from article import urls as article_users
from article import urls as user_urls 

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/article/', include(article_users)),
    path('api/user/', include(user_urls)),
]

Solution 6:[6]

Before using the URL command be sure to first import the url from the module Urls. Then try using the runserver.

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

Solution 7:[7]

Here's an idea: save also the registration information along with the time registered on (in milliseconds for example) and in your splash screen, and if the 7 days are passed: Just resend these registration info and rewrite the customer's token.

Solution 8:[8]

Ask API developer to add an error with 403 error code when the token is expired. When you get a 403 error in API then redirect to the user on the login page.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anto
Solution 2 Fatih DEVECİ
Solution 3 Alienware
Solution 4 Nurhun
Solution 5 jasonshu
Solution 6 JustARandomProgrammer
Solution 7 Ahmed Awad
Solution 8 Prem Chand