call a helper function and create a progress bar on django - 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?

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/

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>

Related

Django respond to a search result in a database from navbar

I'm working on a django app that has a database with various posts. These posts have names and ids. The ids are in sub urls like sitename.com/1. On every page of the django app, I have a navbar which was extended from base.html. The navbar has a search bar which is really a form submission.
When I want to execute a search, I can take in the search data and use filters on it with the given database. However, the views.py has a function for each sub url. With each sub url that has a form on it, the function on views.py is handling the post request for the form.
How am I going to handle the requests from the search bar form? Do I have to make a function that handles it and then copy paste a call for that function at the beginning of the functions for every other page I want to make? Is there an easier way to do this? I've heard of using # but I don't know if that is the right use for this sort of problem.

How to build conversational form with django?

I am trying to build a conversational form with Django.
It will be used in landing page. The form questions will be loaded one by one as user answers them. And there will be some greeting and "human-way" responses to user input (such as "wow! you did a good choice!" after user selects one of the choices from form). The experience and look of the app will be like a real-time chat but user can only select one of the choices from form or upload a file/image.
1. Which technology is better to use for it? I am planning to do it with Fetch.
2. Since I want it to work without page reloading, how do I need to load Django forms through Fetch? Do I need to pass elements of it with JSON and construct it in client-side or can I just pass it as an html with {{form.as_p}} and display it in HTML?
Does these options make difference in matter of security?
I do not know anything about Fetch, but anyway I think it must be constructed clientside, but at first I would simply display the form in a template to get the ids of its fields and then use it in clientside code.
What about security - you'll need to pass the csrf token via your form.

Form in Footer and multi form in Django

Hello Is there any way to accept a input from footer? I have a footer which is for Newsletter signup. Users insert their mail there. How could I accept that data? Do I need to send form via views every time. or there is a way to accept form from that included template code?
Well I also have a feedback form in the footer which in included in all the pages I want the feedback to be stored in my DB. I cannot figure out how I can accept the form data from all pages. (Sending forms in all page through views is possible But I think there is A easy (good Looking) idea) and also There are more them one Post method. I really don't know how to Explain. But I expect you can understand me.
I breakdown the your multiform/newsletter signup in steps as below:
Define view the post view
Add route to the url.py
In your html template add action in form tag example for
Validate input in your defined view
Save it.
That's it.

Cross-domain redirect to a partially filled form on Django web application

I have an HTML form in my Django web application (NOT implemented using Django forms) that does POST request.
Now I want to implement a feature so that other web apps, not necessarily django, from different domains, can send some data to my application and get redirected to the web page with this form, partially filled with that data (the data can be JSON).
Besides redirecting, after the user clicks submit on my form, I would also want to send a message to the other server with some short text information.
I am not sure what is the best way to implement this. REST interface like Piston?
Could you give me some general directions I should follow?
You should create a view that handles the POST data from the form and the external web apps.
You should be able to check whether the data you are getting in the view is coming from your site or another by checking request.META['HTTP_REFERER'].
If it is from your site, you can just handle the form as you usually would.
However if it is from an external site, you would instead render the template with the form in it. You can put the information you got from the external site into the context, so you can pre-fill the form in the template.
You should also include a flag in the form to say that this was from an external site, something like:
<input type="hidden" name="external_site_url" value="{{ external_site_url }}">
After that form is submitted, you can check for the existence of external_site_url. If it exists you can then send the message to the other server.
Note, because you want other apps to use your view, you'll have to disable CSRF protection on the view (https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#csrf-protection-should-be-disabled-for-just-a-few-views).
Also, by allowing other apps to use your view, you are opening yourself up to a lot of possible attacks. Be very careful with input validation and only give the view the ability to do the things it really needs -- you don't want an external app to be able to delete entries in your database for example.

Whats the best way to display message after CRUD operation In Django

I have been doing CRUD operation in Django but I am having problem with what to do after successful operation.
Is there any way that i can redirect the user to Home page with small message on top of page like
"Record Successfully added or something else" like Google does with Dismiss hyperlink :)
EDIT:
Actually that a is full documentation and not a simple example.
I could not understand in which template I have to include {{messages}} in. In update form, edit form or home page form?
I don't understand where I need to define messages. I use only generic views so I am confused.
Use the django.contrib.messages framework to get the same messaging style as used in Django admin.
Enable the middleware module by adding 'django.contrib.sessions.middleware.SessionMiddleware' to MIDDLEWARE_CLASSES in settings.py.
In your view, use something like messages.add_message(request, messages.INFO, 'Stuff happened') to record information where it makes sense.
In your template, you'll have a list messages which you can iterate over where it makes sense in your design - the example in the documentation shows a full example which exposes the tags as CSS classes for ease of styling.