1) In polls.urls add a name for the index page (just like for the 'poll_results' case)
--------------
urlpatterns = patterns('',
# ...
name='poll_index'),
2) In polls.init create a module-wide utility-function to reverse the index url:
--------------
from django.core.urlresolvers import reverse
def get_polls_prefix():
return reverse('poll_index').strip('/')
3) Subclass ListView/DetailsView to add a "prefix" value to the template context. For example (PollDetailsView is similar):
--------------
class PollListView(ListView):
def get_context_data(self, **kwargs):
context = super(PollListView, self).get_context_data(**kwargs)
context['prefix'] = polls.get_polls_prefix()
return context
urlpatterns = patterns('',
url(r'^$',
PollListView.as_view(
queryset=Poll.objects.order_by('-pub_date')[:5],
context_object_name='latest_poll_list',
template_name='polls/index.html'),
name='poll_index'),
4) Now, all my web pages use the prefix from the context; e.g. index.html looks like this:
--------------
....
{% for poll in latest_poll_list %}
<li><a href="/{{ prefix }}/{{ poll.id }}/">{{ poll.question }}</a></li>
{% endfor %}
On 1/4/2012 9:20 μμ, Alexandros Karypidis 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.
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)...
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment