I have an endpoint that renders a template that has a list of movies
On the page, there is a button called "Add movie" and an input field to add the name of the movie. When the button is pressed, I want to add a movie of the name to the list of movies and re render the page.
Right now, I am doing it all in JS, with a fetch request, but I can't re render the page like this.
How could I achieve the effect I want?
You can update the page without reloading/re-rendering the whole webpage using AJAX calls and changing a specific element inside the HTML's DOM. You can do this in raw Javascript or use some JS libraries to simplify the code. Here are some libraries you can consider if you've a large'ish project and such requirements might come up again:
jQuery
intercoolerjs.org
unpoly.js
You're rendering on the server-side, so in order to re-render, you need to send data back to the server. One way of doing this creating a form element which posts data to the same page when you click the button. And then on the server side, take the inputted movie, add it to the other movies, and then you can re-render and return.
This answer attempts to provide general guidance, since your question was general.
Related
How do I trigger a fillable pop-up form when the user clicks on an image, on a Django web app that then winds up sorting the following result ?
I couldn't find a solution to that on other websites. Is this something that can be done solely with HTML, CSS, and JS, or do I have to use python to then save the data to the SQLite database?
If someone can suggest or hint at the steps that I need to follow to get this done I would really appreciate it.
okay for the Popup you need some JS. For the Data of the Popup you have two options
If the Data is static for this page you can add it in the Django template
If its dynamic (it depends on user input at this page) you have to load it as a JSON and display it with JS (for example with Django-Rest Framework
I'm busy making a documentation page in my Django project and would like to make a dynamic page where theres a sidenav with links but if you click on a nav item it loads the page within the "master page aka the documentation page" without leaving that page
like so https://www.nbinteract.com/tutorial/tutorial_github_setup.html
I have created the side nav etc and it's working fine and linked my first URL to a nav item but if you click on the nav item, it opens that .html file instead of loading it within the main documentation page.
I would like to find a way to do it with Django only and not JavaScript if possible, any guidance would be appreciated.
Yes, this could be a silly question but please don't flame me about learning how to do stuff :)
The site you linked uses Javascript and you will need to use Javascript as well if you want to update only portions of the client-page with new data from the database.
I am using SqlAlchemy and FLASK to build a web interface that contains more than 50 user input fields. All the data should enter the database at the same time. Currently every input fields are in one html page(you have to scroll down pretty hard), to minimize this scrolling and for better compartmentalization, I would like to divide this one long html into two or more separate htmls. So when user completes filling in the input fields for page 1, I would like for them to click 'next' button to go to page 2 and so on. Since the data needs to go into the database all at once, I want to keep only one 'submit' button.
I was looking over the web regarding this matter but I wasn't sure what to search for.
Any idea or link to documentations will be really helpful.
Thanks in advance.
please follow the link for pagination http://flask.pocoo.org/snippets/44/ if you don't want to use javascript to create views.
I got one page. On the page is a contact form. After clicking the 'send' button I want to change the form containing div to 'thank you for mailing us' div. And stay at the same 'id' on the page.
One way to solve it is to put an another template and load it as 'success url' But this way the whole page is being loaded again and the user lands on top of the page without seeing the confirmation.
Is there a way not to load the page again and to change the div somehow? With jQuery for example? How to do it? Is there even an another way?
Would be great to have some hints. Many thanks in advance!
Using Javascript is the easiest solution. Here is example for using AJAX with forms. Django can detect AJAX requests out of the box via request.is_ajax().
Another option would be to set cookie, redirect to the same url and handle the response differently (depending on given cookie), but that isn't elegant at all.
In Django whenever I have to use site navigation I use the Reverse resolution of URLs. This way I can make django -render- each new html page and pass that page whatever arguments I want to through the views.
However I am wondering how should I do this in web.py. For instance, I have a web.py template that contains a variable $user. At some point in the main webpage a simple button contains a link of the form
Account
which redirects a user to his account page. Now, I need to pass $user on to account.html so that he/she can change his/her details. The problem is that since I can't directly link to account.html cause it's not a static page, how should I go through web.py and use its render method?
Thanks in advance.
It seems to me that the best way to do this would be with a session variable. That kind of variable can be consistent across several pages.