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 Django Class-based Views Classy Views TemplateView

Help in writing django urls using the url() function and regex matching

As in this project

myproject/urls.py
from django.conf.urls import url, include

from . import views

urlpatterns = [
    url(r'^$', views.WelcomeView.as_view()),
    url(r'name/' views.NameView.as_view())
]
myproject/views.py
from django.http import HttpResponse
from django.views.generic import View, TemplateView


class WelcomeView(View):
    def get(self, request):
        return HttpResponse("Treehouse!")

class NameView(TemplateView):
    template_name = 'name.html'

Hmm your regex seems like its missing something that the one above has.

Are you sure you are matching the beginning ^ and the end $ ?

Catch my drift?

Tip:

However, if you were to be using the newest version of Django, you could be using the function path() instead of url(). .The function path() is easier, because you can just do: path('name/', .... ) instead of: url(r'name/$', ....). Nevertheless, for other types of views you would still need to use regexes with the path() function

1 Answer

Hi carlosanroman I corrected the url to read " url(r'^name/$' views.NameView.as_view()) " but i still get an error for this practice exercise

If this is exacly what you typed:" url(r'^name/$' views.NameView.as_view()) " then you might need a comma after the r'^name/$' and before: views.NameView.as_view())

like this: url(r'^name/$', views.NameView.as_view())