Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!