[newbie] -- Regex-matching ("Tango with Django")

89 views
Skip to first unread message

David

unread,
Mar 14, 2014, 8:44:57 PM3/14/14
to django...@googlegroups.com
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

Shawn Milochik

unread,
Mar 14, 2014, 9:06:32 PM3/14/14
to django...@googlegroups.com
You're meant to only have one include that redirects URLs beginning with rango to the app's urls.py.

Then, the app's urls.py must not have /rango in the URLs. The idea is that all URLs starting with "rango" get the "rango" stripped off and passed to the rango app, which itself has / and /about.


Camilo Torres

unread,
Mar 15, 2014, 1:08:02 PM3/15/14
to django...@googlegroups.com, ld...@gmx.net
Hello,

To point the about view to  http://127.0.0.1:8000/about/, in you project/urls.py, you should change:
url(r'^about/', include('rango.urls')),  # ADD THIS NEW TUPLE!
to
url(r'^about/',  rango.views.about, name='About page'),
(of course, you have to import rango.views)

With your current configuration, these two links:
points to the same as:

Because you included the same urls for 'rango/' and 'about/'

Yours,
Camilo
Reply all
Reply to author
Forward
0 new messages