Django respond to a search result in a database from navbar - django

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.

Related

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.

How to append dyanmic url pattern at the beginning of fixed tail in django urls?

I want to give AJAX call to dyanmic urls in django to serve images. An individual profile can have it's own photos. Then team of individuals will display photos of all members and main page may highlight different random images from different teams. So, I can have following set of urls in django:
^home/images/
^home/teams/1/images/
^home/teams/1/user/2/images/
I'll be using AJAX request to send details like team id, user id etc. So, there'll be only one view that'll handle any request related to viewing images. How can I route all above url request to this single view with single url pattern.
To be specific, How can I construct dynamic urlpattern in django which will allow me to append '/images/' to any url at the run time? Means visitor should be routed to the same view whether he visits any of the above url?
If there is any flaw in my logic or approach, please correct me as I am not expert in django. I'm using django 1.9 version.

Django - Is it possible to show loader until a start page is loaded?

My view function takes a long time until returning a template. So, I'd like to show something to a user while running the function.
Is it possible to show loader until a start page is loaded?
What's important is the loader should be shown when loading the first page after first visit of a user?
Thank you for reading my question.
You would have to fetch the page using JS. You could use something like Intercooler or PJAX which provides HTML attributes that show a spinning animation while loading the content via AJAX.
A better solution would be to make your page faster. There are several things you should consider:
Check that all Model fields that you are using for filtering or sorting have set db_index=True unless the tables are small (few hundred entries) or the fields are already unique or foreign/primary keys. Also check that your DB does sorting and merging in RAM not on disk (== the DB has enough RAM resources and has also the correct configuration to use it).
Sometimes, if you show a list of model instances you end up making separate DB requests per row if you access related models in your template. Again, check which statements your DB executes and have a look at Django's select_related, prefetch_related, values and values_list methods that can dramatically increase lookup performance. Make sure your template context contains all necessary data and only the necessary data (e.g. pageing, how much, or maybe you should consider a search index like SOLR or Elastic which can be integrated nicely via Django Haystack).
Load everything except heavy data at once in your main view, which also includes JS. The JS then uses AJAX to load the rest from a second Django view which returns an HTML snippet that your JS simply adds to the DOM.
It really depends on how comfortable you are with JS and how much you want to stick to HTML to make as much use of Django as possible (thinking of Django Forms for example). But first, tune your DB setup (disclaimer: I have written that article).
it's better to make a request with javascript to your Django endpoint, until you get a response back from your server you should show your loader, and when you get the response back successfully you should make display: none for loader and mak display: block for your loaded content
Create a function in views.py and send JsonResponse. URL example: http://localhost:8000/somedata
Render any other HTMLlet's say it's index.html. URL example: http://localhost:8000/home
That index.html file need to have some JavaScript, let's say main.js
In main.js make a request to http://localhost:8000/somedata and fetch data. Use async javascript that way you can easily track fetched data or not

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.

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