views:

53

answers:

1

I was trying to integrate django-voting into my project following the RedditStyleVoting instruction.

In my urls.py, i did something like this:

    url(r'^sections/(?P<object_id>\d+)/(?P<direction>up|down|clear)vote/?$',
    vote_on_object,
    dict(
        model=Section,
        template_object_name='section',
        template_name='script/section_confirm_vote.html',
        allow_xmlhttprequest=True
        ),
    name="section_vote",

then, in my template:

    {% vote_by_user user on section as vote %}
{% score_for_object section as score %}

<form class="sectionvote" id="sectionup{{ section.id }}"{% if vote and vote.is_upvote %} action="{% url section_vote object_id=section.id, direction="clear" %}"{% else %} action="{% url section_vote object_id=section.id, direction="up" %}"{% endif %} method="POST">
<input type="image" id="sectionuparrow{{ section.id }}" src="{{ MEDIA_URL }}/aup{% if vote and vote.is_upvote %}mod{% else %}grey{% endif %}.png"></form>

{{ score.score|default:0 }}

<form class="sectionvote" id="sectiondown{{ section.id }}"{% if vote and vote.is_downvote %} action="{% url section_vote object_id=section.id, direction="clear" %}"{% else %} action="{% url section_vote object_id=section.id, direction="down" %}"{% endif %} method="POST"> 
<input type="image" id="sectiondownarrow{{ section.id }}" src="{{ MEDIA_URL }}/adown{% if vote and vote.is_downvote %}mod{% else %}grey{% endif %}.png"></form>

It takes over 1.3s to load the page, but by hard coding it like this:

<form class="sectionvote" id="sectionup{{ section.id }}"{% if vote and vote.is_upvote %} action="sections/{{section.id}}/clearvote/"{% else %} action="sections/{{section.id}}/clearvote/"{% endif %} method="POST">

<form class="sectionvote" id="sectiondown{{ section.id }}"{% if vote and vote.is_downvote %} action="sections/{{section.id}}/clearvote/"{% else %} action="sections/{{section.id}}/downvote/"{% endif %} method="POST"> 

I got 50ms. Just avoid the url tag resolving stuff I got a 20+ times performance improvement. Is there something I did wrong? If not, then what's the best practice here, should we do things the right way or the fast way?

+5  A: 

If you're using the development version, there has recently been a regression which has massively increased the time it takes to reverse-lookup URLs - see the ticket here. It is scheduled to be fixed before the final release of 1.2.

Daniel Roseman
Yes, I'm using dev version. Thanks for pointing it out.
zxygentoo
Should just add: that ticket has now been fixed, so if you svn up it should be faster.
Daniel Roseman
Just did, same view, 32ms. One less bug to 1.2rc :)Thanks again, Daniel.
zxygentoo