On Sun, Apr 1, 2012 at 9:20 PM, Alexandros Karypidis <akary...@yahoo.gr> wrote:
> Hi,
>
> My fondness of Python (while scripting things for work) grew to the point
> where I decided to try web development with it. Having (apparently) nothing
> better to do on a Sunday afternoon, I went through the Django tutorials.
>
> The tutorial goes to some length to keep the "polls" app self-contained and
> easily embeddable into the mysite project. However, it seems to do so only
> for the back-end logic and not so much for the front-end UI.
>
> In part 3
> (https://docs.djangoproject.com/en/1.4/intro/tutorial03/#decoupling-the-urlconfs)
> it rewrites the URLConfs to "mount" the app under some site-specific prefix
> (/polls).
>
> urlpatterns = patterns('',
>     url(r'^polls/', include('polls.urls')),
>
> I quote from the tutorial:
>
> "The idea behind include() and URLconf decoupling is to make it easy to
> plug-and-play URLs. Now that polls are in their own URLconf, they can be
> placed under "/polls/", or under "/fun_polls/", or under "/content/polls/",
> or any other path root, and the app will still work."
>
> Ironically, it does not seem to be bothered with the HTML side of things.
> Therefore:
>
> 1) index.html produces a hard-coded list of polls, which MUST be mounted at
> "/polls", with:
>     {% for poll in latest_poll_list %}
>         <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>
>     {% endfor %}
> 2) detail.html routes the voting action to "/polls" POST
>     <form action="/polls/{{ poll.id }}/vote/" method="post">
> 3) results.html takes you back to voting in "/polls"
>     <a href="/polls/{{ poll.id }}/">Vote again?</a>
>
> As I suspected, changing "mysite.urls" to this:
>
>     urlpatterns = patterns('',
>         url(r'^fun_polls/', include('polls.urls')),
>
> ... broke everything which was carefully crafted on the application-code
> side of things.
>

The tutorial is just omitting some common steps to make it simpler,
which at the same time makes it less resilient and correct.

When you setup your urlconf, you should explicitly name each URL:

  urlpatterns = patterns('',
     url(r'^$', 'foo.homepage', name='homepage'),
     url(r'^about$', 'foo.about', name='about'),
     url(r'^polls/(?P<poll_id>\d+)$', 'polls.view_poll', name='view_poll'),
  )

Then, in your templates, you refer to the URLs by name, and ask Django
to sort out what the URL is:

  {% for poll in polls %}
   <li><a href="{% url view_poll poll.id %}">{{ poll }}</a></li>
  {% endfor %}

Now, regardless of how the urlconf is tweaked, the generated HTML will
always be correct.

See the docs:

https://docs.djangoproject.com/en/1.4/topics/http/urls/#id2
https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#url

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to