How to make use of two buttons with two different actions in the same form?
I have a form that has two buttons/input.
Button1 saves the page and refresh it.
Button2 saves the page and goes to another url.
Button1 is working with type="submit" and then view.py takes the data, saves them and refresh the data.
My problem is with Button2. I added it with formaction="{% url 'team_area:home' %}" and actually redirect me but the problem is that it doesn't save the data. Button1 still works properly.
Is it possible to have some sort of request.the_id_of_pressed_button to use in view.py?
If it can be helpful here are my files involved:
modify_players.html
<h1>AREA SQUADRA</h1>
<form method="post" action="">
{% csrf_token %}
<h2>Giocatori</h2>
{{ player_formset.management_form }}
{% for player_form in player_formset %}
{% if forloop.last %}
{% if not forloop.first %}
<input type="submit" value="Salva" formaction="{% url 'team_area:home' %}">
{% endif %}
<h5>Nuovo giocatore:</h5>
{% endif %}
{% for field in player_form %}
{% if forloop.revcounter == 2 %}
{{ field }}
{% elif forloop.parentloop.last and forloop.last%}
{% else %}
{{ field.label_tag }} {{ field }}
{% endif %}
{% endfor %}
<br>
{% endfor %}
<input type="submit" value="Aggiungi">
</form>
views.py
@login_required(login_url="/accounts/login/")
def modify_players(request):
if request.user.team is not None:
PlayerFormSet = modelformset_factory(Player, form=PlayerForm, extra=1, can_delete=True,)
if request.method == "POST":
player_formset = PlayerFormSet(request.POST, request.FILES, queryset=Player.objects.all().filter(team=request.user.team),)
for player_form in player_formset:
if player_form.is_valid():
player = player_form.save(commit=False)
player.team = request.user.team
if player_formset.is_valid():
player_formset.save()
return redirect('team_area:modify_players')
else:
player_formset = PlayerFormSet(queryset=Player.objects.all().filter(team=request.user.team))
return render(request, 'team_area/modify_players.html', {'player_formset': player_formset})
else:
return redirect('team_area:home')
python html django forms django-forms
add a comment |
I have a form that has two buttons/input.
Button1 saves the page and refresh it.
Button2 saves the page and goes to another url.
Button1 is working with type="submit" and then view.py takes the data, saves them and refresh the data.
My problem is with Button2. I added it with formaction="{% url 'team_area:home' %}" and actually redirect me but the problem is that it doesn't save the data. Button1 still works properly.
Is it possible to have some sort of request.the_id_of_pressed_button to use in view.py?
If it can be helpful here are my files involved:
modify_players.html
<h1>AREA SQUADRA</h1>
<form method="post" action="">
{% csrf_token %}
<h2>Giocatori</h2>
{{ player_formset.management_form }}
{% for player_form in player_formset %}
{% if forloop.last %}
{% if not forloop.first %}
<input type="submit" value="Salva" formaction="{% url 'team_area:home' %}">
{% endif %}
<h5>Nuovo giocatore:</h5>
{% endif %}
{% for field in player_form %}
{% if forloop.revcounter == 2 %}
{{ field }}
{% elif forloop.parentloop.last and forloop.last%}
{% else %}
{{ field.label_tag }} {{ field }}
{% endif %}
{% endfor %}
<br>
{% endfor %}
<input type="submit" value="Aggiungi">
</form>
views.py
@login_required(login_url="/accounts/login/")
def modify_players(request):
if request.user.team is not None:
PlayerFormSet = modelformset_factory(Player, form=PlayerForm, extra=1, can_delete=True,)
if request.method == "POST":
player_formset = PlayerFormSet(request.POST, request.FILES, queryset=Player.objects.all().filter(team=request.user.team),)
for player_form in player_formset:
if player_form.is_valid():
player = player_form.save(commit=False)
player.team = request.user.team
if player_formset.is_valid():
player_formset.save()
return redirect('team_area:modify_players')
else:
player_formset = PlayerFormSet(queryset=Player.objects.all().filter(team=request.user.team))
return render(request, 'team_area/modify_players.html', {'player_formset': player_formset})
else:
return redirect('team_area:home')
python html django forms django-forms
What are you trying to do? Why do you need two submit buttons? I ask because there might be another, better solution than adding two buttons.
– xyres
Dec 29 '18 at 16:45
@xyres I need that when I press them both save the data but the first one take me to a page, the second to another page
– Lorenzo Fiamingo
Dec 29 '18 at 17:07
The fact is similar to how admin Django app modifies models. There are the buttons 'Save' and 'Save and continue editing'
– Lorenzo Fiamingo
Dec 29 '18 at 17:10
add a comment |
I have a form that has two buttons/input.
Button1 saves the page and refresh it.
Button2 saves the page and goes to another url.
Button1 is working with type="submit" and then view.py takes the data, saves them and refresh the data.
My problem is with Button2. I added it with formaction="{% url 'team_area:home' %}" and actually redirect me but the problem is that it doesn't save the data. Button1 still works properly.
Is it possible to have some sort of request.the_id_of_pressed_button to use in view.py?
If it can be helpful here are my files involved:
modify_players.html
<h1>AREA SQUADRA</h1>
<form method="post" action="">
{% csrf_token %}
<h2>Giocatori</h2>
{{ player_formset.management_form }}
{% for player_form in player_formset %}
{% if forloop.last %}
{% if not forloop.first %}
<input type="submit" value="Salva" formaction="{% url 'team_area:home' %}">
{% endif %}
<h5>Nuovo giocatore:</h5>
{% endif %}
{% for field in player_form %}
{% if forloop.revcounter == 2 %}
{{ field }}
{% elif forloop.parentloop.last and forloop.last%}
{% else %}
{{ field.label_tag }} {{ field }}
{% endif %}
{% endfor %}
<br>
{% endfor %}
<input type="submit" value="Aggiungi">
</form>
views.py
@login_required(login_url="/accounts/login/")
def modify_players(request):
if request.user.team is not None:
PlayerFormSet = modelformset_factory(Player, form=PlayerForm, extra=1, can_delete=True,)
if request.method == "POST":
player_formset = PlayerFormSet(request.POST, request.FILES, queryset=Player.objects.all().filter(team=request.user.team),)
for player_form in player_formset:
if player_form.is_valid():
player = player_form.save(commit=False)
player.team = request.user.team
if player_formset.is_valid():
player_formset.save()
return redirect('team_area:modify_players')
else:
player_formset = PlayerFormSet(queryset=Player.objects.all().filter(team=request.user.team))
return render(request, 'team_area/modify_players.html', {'player_formset': player_formset})
else:
return redirect('team_area:home')
python html django forms django-forms
I have a form that has two buttons/input.
Button1 saves the page and refresh it.
Button2 saves the page and goes to another url.
Button1 is working with type="submit" and then view.py takes the data, saves them and refresh the data.
My problem is with Button2. I added it with formaction="{% url 'team_area:home' %}" and actually redirect me but the problem is that it doesn't save the data. Button1 still works properly.
Is it possible to have some sort of request.the_id_of_pressed_button to use in view.py?
If it can be helpful here are my files involved:
modify_players.html
<h1>AREA SQUADRA</h1>
<form method="post" action="">
{% csrf_token %}
<h2>Giocatori</h2>
{{ player_formset.management_form }}
{% for player_form in player_formset %}
{% if forloop.last %}
{% if not forloop.first %}
<input type="submit" value="Salva" formaction="{% url 'team_area:home' %}">
{% endif %}
<h5>Nuovo giocatore:</h5>
{% endif %}
{% for field in player_form %}
{% if forloop.revcounter == 2 %}
{{ field }}
{% elif forloop.parentloop.last and forloop.last%}
{% else %}
{{ field.label_tag }} {{ field }}
{% endif %}
{% endfor %}
<br>
{% endfor %}
<input type="submit" value="Aggiungi">
</form>
views.py
@login_required(login_url="/accounts/login/")
def modify_players(request):
if request.user.team is not None:
PlayerFormSet = modelformset_factory(Player, form=PlayerForm, extra=1, can_delete=True,)
if request.method == "POST":
player_formset = PlayerFormSet(request.POST, request.FILES, queryset=Player.objects.all().filter(team=request.user.team),)
for player_form in player_formset:
if player_form.is_valid():
player = player_form.save(commit=False)
player.team = request.user.team
if player_formset.is_valid():
player_formset.save()
return redirect('team_area:modify_players')
else:
player_formset = PlayerFormSet(queryset=Player.objects.all().filter(team=request.user.team))
return render(request, 'team_area/modify_players.html', {'player_formset': player_formset})
else:
return redirect('team_area:home')
python html django forms django-forms
python html django forms django-forms
edited Dec 29 '18 at 17:12
Bindiya H
12757
12757
asked Dec 29 '18 at 15:57
Lorenzo FiamingoLorenzo Fiamingo
809
809
What are you trying to do? Why do you need two submit buttons? I ask because there might be another, better solution than adding two buttons.
– xyres
Dec 29 '18 at 16:45
@xyres I need that when I press them both save the data but the first one take me to a page, the second to another page
– Lorenzo Fiamingo
Dec 29 '18 at 17:07
The fact is similar to how admin Django app modifies models. There are the buttons 'Save' and 'Save and continue editing'
– Lorenzo Fiamingo
Dec 29 '18 at 17:10
add a comment |
What are you trying to do? Why do you need two submit buttons? I ask because there might be another, better solution than adding two buttons.
– xyres
Dec 29 '18 at 16:45
@xyres I need that when I press them both save the data but the first one take me to a page, the second to another page
– Lorenzo Fiamingo
Dec 29 '18 at 17:07
The fact is similar to how admin Django app modifies models. There are the buttons 'Save' and 'Save and continue editing'
– Lorenzo Fiamingo
Dec 29 '18 at 17:10
What are you trying to do? Why do you need two submit buttons? I ask because there might be another, better solution than adding two buttons.
– xyres
Dec 29 '18 at 16:45
What are you trying to do? Why do you need two submit buttons? I ask because there might be another, better solution than adding two buttons.
– xyres
Dec 29 '18 at 16:45
@xyres I need that when I press them both save the data but the first one take me to a page, the second to another page
– Lorenzo Fiamingo
Dec 29 '18 at 17:07
@xyres I need that when I press them both save the data but the first one take me to a page, the second to another page
– Lorenzo Fiamingo
Dec 29 '18 at 17:07
The fact is similar to how admin Django app modifies models. There are the buttons 'Save' and 'Save and continue editing'
– Lorenzo Fiamingo
Dec 29 '18 at 17:10
The fact is similar to how admin Django app modifies models. There are the buttons 'Save' and 'Save and continue editing'
– Lorenzo Fiamingo
Dec 29 '18 at 17:10
add a comment |
1 Answer
1
active
oldest
votes
Your comments cleared up a couple of things.
Remove formaction
from the input
so that both the buttons submit the form to the same view.
Give both the buttons a common name
but different value
. Then in your view, you can differentiate which button was clicked.
html:
<input type="submit" name="submit_button" value="button1">
<input type="submit" name="submit_button" value="button2">
view:
def modify_players(...):
...
# save data
...
submit_button = request.POST.get('submit_button')
if submit_button == 'button1':
# refresh page
else:
# redirect to some page
Just what I was searching for! I would ask you this: if I don't set a name for <input> how is it passed trough request.POST? Can I use the get()?
– Lorenzo Fiamingo
Dec 29 '18 at 17:40
1
@LorenzoFiamingo Without aname
attribute, the value of input isn't sent to the backend. Although, form will be submitted if it istype="submit"
.
– xyres
Dec 29 '18 at 17:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53971058%2fhow-to-make-use-of-two-buttons-with-two-different-actions-in-the-same-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your comments cleared up a couple of things.
Remove formaction
from the input
so that both the buttons submit the form to the same view.
Give both the buttons a common name
but different value
. Then in your view, you can differentiate which button was clicked.
html:
<input type="submit" name="submit_button" value="button1">
<input type="submit" name="submit_button" value="button2">
view:
def modify_players(...):
...
# save data
...
submit_button = request.POST.get('submit_button')
if submit_button == 'button1':
# refresh page
else:
# redirect to some page
Just what I was searching for! I would ask you this: if I don't set a name for <input> how is it passed trough request.POST? Can I use the get()?
– Lorenzo Fiamingo
Dec 29 '18 at 17:40
1
@LorenzoFiamingo Without aname
attribute, the value of input isn't sent to the backend. Although, form will be submitted if it istype="submit"
.
– xyres
Dec 29 '18 at 17:52
add a comment |
Your comments cleared up a couple of things.
Remove formaction
from the input
so that both the buttons submit the form to the same view.
Give both the buttons a common name
but different value
. Then in your view, you can differentiate which button was clicked.
html:
<input type="submit" name="submit_button" value="button1">
<input type="submit" name="submit_button" value="button2">
view:
def modify_players(...):
...
# save data
...
submit_button = request.POST.get('submit_button')
if submit_button == 'button1':
# refresh page
else:
# redirect to some page
Just what I was searching for! I would ask you this: if I don't set a name for <input> how is it passed trough request.POST? Can I use the get()?
– Lorenzo Fiamingo
Dec 29 '18 at 17:40
1
@LorenzoFiamingo Without aname
attribute, the value of input isn't sent to the backend. Although, form will be submitted if it istype="submit"
.
– xyres
Dec 29 '18 at 17:52
add a comment |
Your comments cleared up a couple of things.
Remove formaction
from the input
so that both the buttons submit the form to the same view.
Give both the buttons a common name
but different value
. Then in your view, you can differentiate which button was clicked.
html:
<input type="submit" name="submit_button" value="button1">
<input type="submit" name="submit_button" value="button2">
view:
def modify_players(...):
...
# save data
...
submit_button = request.POST.get('submit_button')
if submit_button == 'button1':
# refresh page
else:
# redirect to some page
Your comments cleared up a couple of things.
Remove formaction
from the input
so that both the buttons submit the form to the same view.
Give both the buttons a common name
but different value
. Then in your view, you can differentiate which button was clicked.
html:
<input type="submit" name="submit_button" value="button1">
<input type="submit" name="submit_button" value="button2">
view:
def modify_players(...):
...
# save data
...
submit_button = request.POST.get('submit_button')
if submit_button == 'button1':
# refresh page
else:
# redirect to some page
answered Dec 29 '18 at 17:20
xyresxyres
9,36232345
9,36232345
Just what I was searching for! I would ask you this: if I don't set a name for <input> how is it passed trough request.POST? Can I use the get()?
– Lorenzo Fiamingo
Dec 29 '18 at 17:40
1
@LorenzoFiamingo Without aname
attribute, the value of input isn't sent to the backend. Although, form will be submitted if it istype="submit"
.
– xyres
Dec 29 '18 at 17:52
add a comment |
Just what I was searching for! I would ask you this: if I don't set a name for <input> how is it passed trough request.POST? Can I use the get()?
– Lorenzo Fiamingo
Dec 29 '18 at 17:40
1
@LorenzoFiamingo Without aname
attribute, the value of input isn't sent to the backend. Although, form will be submitted if it istype="submit"
.
– xyres
Dec 29 '18 at 17:52
Just what I was searching for! I would ask you this: if I don't set a name for <input> how is it passed trough request.POST? Can I use the get()?
– Lorenzo Fiamingo
Dec 29 '18 at 17:40
Just what I was searching for! I would ask you this: if I don't set a name for <input> how is it passed trough request.POST? Can I use the get()?
– Lorenzo Fiamingo
Dec 29 '18 at 17:40
1
1
@LorenzoFiamingo Without a
name
attribute, the value of input isn't sent to the backend. Although, form will be submitted if it is type="submit"
.– xyres
Dec 29 '18 at 17:52
@LorenzoFiamingo Without a
name
attribute, the value of input isn't sent to the backend. Although, form will be submitted if it is type="submit"
.– xyres
Dec 29 '18 at 17:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53971058%2fhow-to-make-use-of-two-buttons-with-two-different-actions-in-the-same-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What are you trying to do? Why do you need two submit buttons? I ask because there might be another, better solution than adding two buttons.
– xyres
Dec 29 '18 at 16:45
@xyres I need that when I press them both save the data but the first one take me to a page, the second to another page
– Lorenzo Fiamingo
Dec 29 '18 at 17:07
The fact is similar to how admin Django app modifies models. There are the buttons 'Save' and 'Save and continue editing'
– Lorenzo Fiamingo
Dec 29 '18 at 17:10