2010-04-09 71 views
3

我試圖將django-voting整合到我的項目中,遵循RedditStyleVoting指令。django url標記性能

在我的urls.py,我做了這樣的事情:

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", 

然後,在我的模板:

{% 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> 

它接管1.3s加載網頁,而是通過硬編碼它是這樣的:

<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"> 

我得到了50ms。只要避免url標籤解決的東西,我得到了20倍以上的性能改進。 有什麼我做錯了嗎?如果不是的話,那麼我們應該採取什麼方式或快速的方式來實現最佳做法?

回答

5

如果您使用的是開發版本,最近出現了一種迴歸,大幅增加了反向查找URL所需的時間 - 請參閱the ticket here。它計劃在最終版本1.2之前修復。

+0

是的,我正在使用dev版本。感謝您指出。 – zxygentoo 2010-04-09 17:47:23

+0

應該只是補充一點:該票據現在已經修復,所以如果你認爲它應該更快。 – 2010-04-10 14:44:08

+0

只是做了,相同的看法,32ms。少一個bug到1.2rc :) 再次感謝Daniel。 – zxygentoo 2010-04-11 12:43:02