Reputation: 59
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
Upvotes: 0
Views: 882
Reputation: 6711
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 *
Upvotes: 0
Reputation: 31404
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.
Upvotes: 2