Using pycharm community python3.6.2 Django 2.0.3
views.py
from django.http import HttpResponse
def hello_world(request):
return HttpResponse('Hello World')
urls.py
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views, hello_world),
]
i Tried to figure it out but missing something.
error while running on pycharm
urls.py", line 8, in
url(r'^$', views, hello_world),
NameError: name 'hello_world' is not defined
The error is telling you that there is no variable such as hello_world defined. You need to change it to:
url(r'^$', views.hello_world)
Where views is the views module that you have imported at the top.
This line of code is wrong
url(r'^$', views, hello_world)
You just imported the view which is the file view.py. Now you need to call the function view, which it is going to be like:
url(r'^$', views.hello_world)
And you might think it is going to be useful to give that url a name so you can use it as a reference in your templates in the future.
url(r'^$', views.hello_world, name='hello-world')
Also, you can import your view.py as follow:
from .views import hello_world
The next is possible as well as suggested in the comments Niayesh Isky, but not encouraged.
from .views import *
Related
when i tried to write the first urlpattern and the first view, i got this error, so i can be able to access to the authentification template, i have no idea what can be the source of this error
# my gp/urls.py
from django.contrib import admin
from django.conf.urls import url
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^$', views.index, name='index'),
]
# my views.py
from django.shortcuts import render
def index(request):
return render(request,'gp/index.html')
when i try to run the server this is the error i get
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf
'gp.urls' does not appear to have any patterns in it. If you see valid
patterns in the file then the issue is probably caused by a circular
import.
this is my program tree
gp
apps
conge
organisation
personnel
stage
gp
pycache
init.py
settings.py
urls.py
views.py
wsgi.py
static
templates
gp
index.html
db.sqlte3
manage.py
# my gp/urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
]
# my views.py
from django.shortcuts import render
def index(request):
return render(request,'gp/index.html', {})
what i edited is:
instead of from django.conf.urls import url i wrote from
django.urls import path
added {} in render function (its optional but just in case :) )
As stated in here: https://code.djangoproject.com/ticket/30728#no1
Maybe try to use latest Python version. Python 3.9 able to work
[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!]
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),
]
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')),
]
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
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)),
]
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
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 ??
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
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.
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!
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?
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
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.
I am trying to access urlpatterns variable from url.py in view.py. but it gives error
Checkout my following code.
from testproject.urls import *
print urlpatterns
it gives following error.
NameError: name 'urlpattern' is not defined
Can anybody help ?
It seems that you are trying to print urlpattern instead of urlpatterns. Are you sure this is correct?
To print urlpatterns you have to:
From urls.py you should be able to print it directly because urlpatterns is defined here.
From views.py you have to import the right urls.py file based on where it is. If you want to import the main urls.py then from project_name.urls import urlpatterns. If you want to import the urls.py of an app then from project_name.app_name.urls import urlpatterns.
NOTE: Remember explicit is always better than implicit, so instead of from testproject.urls import * is better from testproject.urls import urlpatterns