Reputation: 361
In my HTML-file I have a submit button (click_button) to do stuff and a submit button for a Django form (form_submit). All submits are redirected to one View-function (get_post_from_submit). If using the return render() function, I always have to return the context including the form, but I have no information of the form when I pressed the click_button.
Is there any way to not return the form again?
home.html
....
<body>
{% load bootstrap4 %}
<form action="" method="POST" novalidate>
{% csrf_token %}
{% bootstrap_form form %}
</form>
<form action="" method="POST" novalidate>
{% csrf_token %}
<button class="btn btn-primary" name="click_button" type="submit">Grafik</button>
</form>
</body>
....
views.py
def get_post_from_submit(request):
if request.method == 'POST':
if "book_field" in request.POST:
# Do Stuff
form = form_book(request.POST)
if "click_button" in request.POST:
# Do Stuff
# Here the form always gets a reset, because I have no Info about the form in POST but it is needed as context...
form = form_book()
return render(request, 'home.html',{"form": form})
Upvotes: 0
Views: 30
Reputation: 2798
One method would be to encapsulate just one form
tag across both sections.
<form action="" method="POST" novalidate>
{% bootstrap_form form %}
<input type="submit" name="action" value="FormSubmit"/>
{% csrf_token %}
<input type="submit" name="action" value="Grafik"/>
</form>
Using name="action"
you can differentiate the POST action in the view like so:
def get_post_from_submit(request):
if request.method == 'POST':
form = form_book(request.POST)
if request.POST['action'] == 'FormSubmit':
[...]
elif request.POST['action'] == 'Grafik':
[...]
return render(request, 'home.html',{"form": form})
The form_book
will no be able to retain the form data whenever the Grafik
POST type is send to the view.
Upvotes: 1