Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Getting started with Django

I am running Django on my own code editor and am following along with the Django tutorial.

                        Here is my code...

(views.py)
from django.http import HttpResponse

def hello_world(request): return HttpResponse('Hello World')

(urls.py) from django.contrib import admin from django.urls import path from . import views

urlpatterns = [ path('admin/', admin.site.urls), url(r'^$', views.hello_world), ]

   When I run localhost, nothing seems to happen. When I check cmd the following error pops up...

line 22, in <module> url(r'^$', views.hello_world), NameError: name 'url' is not defined

2 Answers

diogorferreira
diogorferreira
19,363 Points

From what I can see it looks like you haven't imported url

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

urlpatterns = [ path('admin/', admin.site.urls), 
              url(r'^$', views.hello_world), 
]
  • It has only imported path from django.urls import path

  • You would need to add from django.conf.urls import url

But make sure you check which version of django you are using, I believe the newer versions don't support it. This is how you can do it in newer versions:

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

The cmd now spits out django.db.utils.OperationalError: (1046, 'No database selected')

Thank you for your time!

diogorferreira
diogorferreira
19,363 Points

No problem, I've gone through so many of these errors too when doing the Django course that half of my time is spent trying to fix them. I'd recommend using workspaces for the rest of the course because then you aren't using a different version to them and there's a lower chance your program will break, other than small spelling mistakes.

I have no idea what that error can be but I'll try look into it.

Thank you!