Sunday, April 1, 2012

URL encoding, templates and apps: are apps not portlets?

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.

I am currently tinkering to define a "app_prefix" variable in the code and trying to have the templates read it from the context, so that things become more modular...

I thought apps in Django were supposed to be like "Wicket components" or "Java portlets" (i.e. carry their UI along). Am I letting my Java background take me to the wrong direction here? Obviously, if someone were to use the polls code mounted under "/fun_polls" they could create their own version of "index/details/results.html" that use "/fun_polls" for their prefix (and even display things differently)...

No comments:

Post a Comment