Django url shortcut performance

I tried to integrate django-voting into my project following the RedditStyleVoting instructions .

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>

To load a page, you need to download 1.3s, but using hard coding it looks 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 50 ms. Just avoid URL handling, I got a 20x performance improvement. Something I did wrong? If not, which of the best practices is here, should we do things in a way rightor a quick way?

+3
source share
1 answer

, , , URL- - . . 1.2.

+5

Source: https://habr.com/ru/post/1740491/


All Articles