Django HTTP post to same URL - json

What is the "proper" django way to do an HTTP POST to the same page when a button is clicked in that page?
I've got a django app which contains a page with two different buttons. Each button does a different thing but the results of the button press are returned in JSON format which then gets used to update the UI on the page.
I can obviously have each button submit to its own view and do it that way. But I can also make the page view respond to the button presses and detect whether the request is a POST or not.
Then there is the JSON mixin stuff - is it worth trying to use that somehow?
I've got it all working - I'd just like to know what the "proper" way to do it would be.
Any ideas?

As Mikko Ohtamaa said, common practice is to check in your view which button was pressed. e.g.:
template.html:
<form method="post" action="">
{{ obj_form.as_p }}
<button name="action1" value="1" type="submit">
<button name="action2" value="1" type="submit">
</form>
views.py:
if 'action1' in self.request.POST:
form = Action1Form(request.POST)
elif 'action2' in self.request.POST:
form = Action2Form(request.POST)
Using several forms in a single view is more convenient when you have one HTML form with different actions. If you have separate forms (or don't have them at all), I suggest using separate views for each action.

Related

What is the correct method to link a homepage to multiple views in a django app

As the titles, I'm looking for the correct way to "link" views to a homepage, so whenever I press one of the buttons in the homepage, it will redirect me to a view related to a form.
Right now my django app is composed by multiple views where each one of them are forms that have their own .html but I'm having a hard time to understand or find a way to make a homepage where I have links to specific views and whenever I press the submit button inside the respective form, it would redirect me to the homepage.
I was thinking that the correct way to do it was making a new view that will take the role of my "homepage" but I don't know if its possible to have multiple views related to the same view
Note: I understand that coding an example for this would take a while, I am just making this question so I can get a better idea and start looking in the right direction
I am not sure that I fully understand your question. However, hopefully the below information may help you get a better idea and start looking in the right direction.
redirect()
The redirect() function in Django can be used to redirect to another view/URL from within a view function.
Documentation: https://docs.djangoproject.com/en/4.0/topics/http/shortcuts/#redirect.
{% url %}
The url template function can be used to link to other views/web pages from within a Django template. This prevents the need to hard-code URLs into web pages.
Documentation: https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#url.
If I am not wrong you want to redirect to homepage after submitting a form.
you can check this link how to add action in form though it's w3school tutorial in HTML form.
So, here I am adding code from django documentation.
<form action="/your-name/" method="post">
<label for="your_name">Your name: </label>
<input id="your_name" type="text" name="your_name" value="{{ current_name }}">
<input type="submit" value="OK">
So, you can add action to homepage. just add the page URL name in the action tag ---
suppose you homepage URL is like below ---
path('index/', views.index, name='home'),
Then add home to the action place ---
<form action="{% url 'home' %}" method="post">
<label for="your_name">Your name: </label>
<input id="your_name" type="text" name="your_name" value="{{ current_name }}">
<input type="submit" value="OK">
That will work for sure.
For more information you can go through the below references ---
you will get this in django official documentation as well.
check this link
And here is the whole forms documentation of django
check this link
Still if am wrong or didn't understand you question then please correct me.
Thanks for your question.

Stop page refresh on button click in spring boot

I am having a form and in that checkbox and button is present, on click of button, the data are submitting and also inserting in database, but my page is refreshing, I want my data should submit and insert in database and also page should not refresh in thymeleaf spring boot. The values in checkbox are displaying in loop. My current code is:
<form class="myform myform-newaccount" action="#" id="myForm"
th:action="#{/authorList?buttonValue=pluginlist}"
th:object="${userModifyDto}" method="post">
<input type="checkbox" name="applicationId" th:field="*{applicationIds}" id="myCheck"
th:value="${option.applicationId}" />
<input type="submit" value="send" id="myid"/>
</form>
The default behaviour of server-side rendering technology such as Thymeleaf is that actions always trigger a page refresh. See Form handling with Thymeleaf for more information on that.
That said, there are 2 options:
Use JavaScript (either plain JS or a framework) to do AJAX requests and avoid the page refreshes.
Use hx-boost from the htmx library. This will make it seem like there is no page refresh by intercepting the form submit and doing the request to the server via an AJAX call. See TodoMVC with Thymeleaf and HTMX for more information on that option.

posting html form data redirect

I am posting modal data to a back end using HTML post and action attr. The form post fine and updates the backend as well the only thing is after pressing submit the url changes and takes me to my POST url. How can this be stopped? I want to stay on the same page after clicking submit and just have the backend update without being taken to my POST url.
<form method="POST"id="goForm"action="http://post_endpoint"enctype="application/x-www-form-urlencoded">
Name: <input type="text" value="">
<button type="submit value="submit">Send<button>
</form>
To submit an HTML form without reloading the page, you have to use Javascript.
Here is a similar question with answers: Submit form without page reloading

Activate a search on another website

I am trying to embed the search forms of two other sites into my own site. Here is my site, the forms are at the bottom: http://dsa.dartmouth.edu/.
The DGD search works fine - the user gets directed to the search results page - but the Course Picker one doesn't - the user just gets redirected to a blank search page.
This is my form code:
<form action="http://coursetown.hacktown.cs.dartmouth.edu/search" method="post" class="appSearch">
<input id="search" name="query[title]" placeholder="course title(s)" type="text" role="textbox">
<input type='submit' name='commit' type="submit" value='Search' />
</form>
Is there a way to make it so that the user gets directed to coursetown's search results page when they click search?
The search form on http://coursetown.hacktown.cs.dartmouth.edu/search submits by AJAX so there is no search URL to hit. Furthermore, it seems to use a hidden "authenticity_token" field, which is probably generated server-side on load so you probably need to find out if they have an API you can use for this.
If you can edit that page directly to trigger the search by JavaScript based on URL parameters that would be an option too, not sure what level of control you have over the search page.

html single submit button used for two different tasks

I've a html button which I want for two different tasks on a single press event. First, when I press the submit button it should insert data to the database (which I've done) and the another task is to render to another page. How can I make this button do these two functions simultaneously?
You can do this by specyfing action attribute of form element:
<form method="post" action="page.jsp?action=save">
...
<input type="submit" value="Submit!" />
</form>
After clicking submit button user is redirected to page.jsp (it may be the same page) and in that file you can get value of action variable: if it's save then execute code responsible for writing to db. After that code you place page-rendering code.
It's the simplest solution, but maybe not the most elegant.
"Just" do it then.
someDAO.save(someData);
response.sendRedirect(newURL);
That's basically all you need to execute in your servlet (or JSP if you're still abusing it as a controller).