Dear list,
I am wodering whether my implementation is incorrect or whether there is
a typo in the textbook. My question is how I can make sense of these
instructions: "The regular expression to match about/ is r'^about/'".
I was following the "Tango with Django" book.
In the exercises of chapter 3 I am asked to
"""
Now map the view ['about', which we just created] to /rango/about/. For
this step, you’ll only need to edit the urls.py of the rango application.
"""
Later, the author gives some hints, specifically:
"""
The regular expression to match about/ is r'^about/'
"""
Now, there was no way to do this. I finally managed to get
http://127.0.0.1:8000/rango/ and
http://127.0.0.1:8000/rango/about/
running satisfactorily (my code is below), but I was not even sure this
was intended. The author possibly wanted to create
http://127.0.0.1:8000/about/ instead of
http://127.0.0.1:8000/rango/about/
Thanks for clarifying this issue for me!
Cheers,
David
Here is my project/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'justnow.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
# url(r'^admin/', include(admin.site.urls)),
url(r'^rango/', include('rango.urls')), # ADD THIS NEW TUPLE!
url(r'^about/', include('rango.urls')), # ADD THIS NEW TUPLE!
)
The app urls.py:
from django.conf.urls import patterns, include, url
from rango import views
urlpatterns = patterns('',
url(r'^$', views.index, name='Rangos index'),
url(r'about', views.about, name='About page'),
)
The app views.py:
from django.http import HttpResponse
def index(request):
return HttpResponse("Rango says hello. <a
href='/rango/about/'>About</a>")
def about(request):
return HttpResponse("Here is the about page. <a
href='/rango/'>Index</a>")
david@ubuntu:~/[...]/django_project$ tree
.
├── manage.py
├── notes.txt
├── rango
│ ├── admin.py
│ ├── admin.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── tests.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
└── tango_with_django_project
├── __init__.py
├── __init__.pyc
├── settings.py
├── settings.pyc
├── urls.py
├── urls.pyc
├── wsgi.py
└── wsgi.pyc