b10n1k
b10n1k

Reputation: 615

call a helper function and create a progress bar on django

I want to make a progress bar for a poll. I use django 1.4.8 for the implementation. The progress bar should display a rercentage of people who have already vote. There is also a helper function which return the sum of users voted.

At first i edit the template and the css to display the bar. After that, i try to use jquery for the actual behavor. But I am lost. I searched on the internet but i could make understanding in a clear manner. I am not sure which is the right way to do it. I dont want to use any other external library.

So, the question is how to call the help function and where i ll use the value to display the proper percentage on the progress bar into the template?

Upvotes: 0

Views: 1571

Answers (2)

b10n1k
b10n1k

Reputation: 615

So to be accurate the helper.py imports register from jingo. This means you call the function instantly into template. Furthermore I used instead of for my purpose.

helper.py

from jingo import register

@register.filter
def get_value():
     return value

into template

<meter min="0" value="{{ <app_name>|get_value }} max="1"></meter>

Upvotes: 0

Anentropic
Anentropic

Reputation: 33853

You need to think about how the web browser and the server (Django) communicate.

The browser makes a 'get' request to a url on the server. In Django you have an urls.py file which determines which view will handle requests to that url. For example there will be a view which renders a template containing the HTML code for the poll form.

When the user submits the poll, the browser makes a 'post' request, sending the form data for the poll to the server - to a specific Django view which handles saving the poll vote to the database. Usually in Django we'd use the same view function for both the get and the post for a form - see this answer for more details: https://stackoverflow.com/a/21114637/202168

It's not clear whether you want to show the progress bar initially, or only after they've submitted their vote. Either way you just need to calculate the relevant values for percentage and sum of users in your Django view and pass those values into the template, which renders the HTML sent back to the browser.

If you're using an HTML5 progress bar you don't even need jQuery. Unless you want a user to see the progress bar changing as other users submit their votes (much more complicated) you don't need any 'ajax' stuff either.

I suggest first you start with the Django tutorial:
https://docs.djangoproject.com/en/dev/intro/tutorial01/

Upvotes: 1

Related Questions