Django request.GET 不包含提交表单按钮的名称
Django request.GET does not contain name of submitted form button
我基本上在上一个问题 (Django request.POST does not contain the name of the button that submitted the form) 中解决了同样的问题,但那里描述的修复不起作用。
我正在使用 ajax,以便在提交后,我会停留在同一页面上。我的页面有两种形式。第一个表单有三个按钮,我在 GET 请求中正确传递了按钮的 "name"。我的第二个表单有两个按钮,但我无法在 GET 请求中传递任一按钮的 "name"(但是,输入字段的 "name" 在 GET 请求中)。我不知道为什么它不起作用。有任何想法吗?
JS 解决方案:
$(document).ready(function(){
console.log("hello from email.js");
$('.email_citations').submit(function(event){
console.log("submit email");
var whichButton = $(".form_button[clicked=true]")[0].value
console.log("WHICH BUTTON: ");
console.log(whichButton);
var email_address = $(this).val();
var data = $(this).serialize();
data += "&" + whichButton;
console.log(data);
$.ajax({
data: data,
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data){
console.log("DATA: ");
console.log(data);
$('.email_sent_message').html(JSON.parse(data).submit_message);
}
});
return false;
});
$(".form_button").click(function() {
$(".form_button").removeAttr("clicked");
$(this).attr("clicked", "true");
});
});
html 表单:
<!-- This form works! -->
<form action="export_options" method="get">
<button type="submit" name="plain" class="button inline large hover" id="exportButton2">Download Plain Text</button>
<button type="submit" name="ris" class="button inline large hover" id="exportButton1">Download RIS File</button>
<button type="submit" name="bibtex" class="button inline large hover" id="exportButton3">Download BibTeX File</button>
</form>
<form class="email_citations" id="EmailCitationsForm" action="email_it" method="get">
<input class="input-text" type="text" size="75" maxlength="75" style="width: 40%; font-size: 16px; margin-right: 5px; height: 50px" name="email_address" placeholder="email address">
<button type="submit" value="selected" name="which_citations" class="form_button button inline large hover" id="exportButton4" >Send My Selected Citations</button>
<button type="submit" value="all" name="which_citations" class="form_button button inline large hover" id="exportButton5">Send All Citations</button>
</form>
<div class="email_sent_message" style="float: left; color: #9e1d2a; font-size: 20px; font-weight: bold">{{ submit_message }}</div>
我的 javascript/ajax 在 email.js 文件中:
$(document).ready(function(){
console.log("hello from email.js");
$('.email_citations').submit(function(event){
console.log("submit email");
var clicked_button = $(".form_button[clicked=true]")[0].name;
console.log(clicked_button);
clicked_button.push($(".form_button[clicked=true]")[0].value);
console.log(clicked_button);
var email_address = $(this).val();
var data = $(this).serialize();
data += "&" + clicked_button;
console.log(data);
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data){
$('.email_sent_message').html(JSON.parse(data).submit_message);
}
});
return false;
});
$(".form_button").click(function() {
$(".form_button").removeAttr("clicked");
$(this).attr("clicked", "true");
});
});`
我的看法:
def email_it(request):
context = RequestContext(request)
if request.method == 'GET':
getdict = request.GET.dict()
form = EmailCitationsForm(request.GET)
if form.is_valid():
context = request.session.get('All_Citations_Context')
if ('which_citations') in getdict.keys():
if 'all' in getdict.values():
text_template = 'email_allCitations.txt'
html_template = 'email_allCitations.html'
text_content = render_to_string(text_template, context, context_instance=RequestContext(request))
html_content = render_to_string(html_template, context, context_instance=RequestContext(request))
msg = EmailMultiAlternatives(subject, text_content, from_email, to)
#msg.attach()
msg.attach_alternative(html_content, "text/html")
msg.send()
submit_message = "Thank you. Your citations have been sent via email."
return http.HttpResponse(json.dumps({'submit_message': submit_message}))
if 'selected' in getdict.values():
text_template = 'email_selectedCitations.txt'
html_template = 'email_selectedCitations.html'
text_content = render_to_string(text_template, context, context_instance=RequestContext(request))
html_content = render_to_string(html_template, context, context_instance=RequestContext(request))
msg = EmailMultiAlternatives(subject, text_content, from_email, to)
msg.attach_alternative(html_content, "text/html")
msg.send()
submit_message = "Your citations have been sent via email."
return http.HttpResponse(json.dumps({'submit_message': submit_message}))
else:
submit_message = "Something went wrong. Sorry, your email did not send."
else:
submit_message = form.errors['email_address']
else:
form = EmailCitationsForm()
submit_message = ""
return http.HttpResponse(json.dumps({'submit_message': submit_message}))
好的,这更像是一个 HTML/jQuery 问题而不是 Django 问题。
首先,您的第一个表单可以正常工作,因为您可能让浏览器处理了提交。如果您使用与提供的代码类似的代码,则它不起作用。
如果你记录 $(this).serialize() 你得到的正是你在后端得到的。查看此答案 jQuery: how to get which button was clicked upon form submission?
<form class="email_citations" id="EmailCitationsForm" action="email_it" method="get">
<input class="input-text" type="text" size="75" maxlength="75" style="width: 40%; font-size: 16px; margin-right: 5px; height: 50px" name="email_address" placeholder="email address">
<button type="submit" value="selected_citations" name="selected_citations" class="form_button button inline large hover" id="exportButton4" >Send My Selected Citations</button>
<button type="submit" value="all_citations" name="all_citations" class="form_buttonbutton inline large hover" id="exportButton5">Send All Citations</button>
</form>
注意我已经删除了 django 位。
$(document).ready(function(){
console.log("hello from email.js");
$('.email_citations').submit(function(event){
console.log("submit email");
var clicked_button = $(".form_button[clicked=true]")[0].name;
console.log(clicked_button);
var email_address = $(this).val();
var data = $(this).serialize();
data += "&" + clicked_button;
console.log(data);
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data){
$('.email_sent_message').html(JSON.parse(data).submit_message);
}
});
return false;
});
$(".form_button").click(function() {
$(".form_button").removeAttr("clicked");
$(this).attr("clicked", "true");
});
});
或在 plunker 中查看 http://plnkr.co/edit/CxPGmxQfeHhtsWiANDJ1
我基本上在上一个问题 (Django request.POST does not contain the name of the button that submitted the form) 中解决了同样的问题,但那里描述的修复不起作用。
我正在使用 ajax,以便在提交后,我会停留在同一页面上。我的页面有两种形式。第一个表单有三个按钮,我在 GET 请求中正确传递了按钮的 "name"。我的第二个表单有两个按钮,但我无法在 GET 请求中传递任一按钮的 "name"(但是,输入字段的 "name" 在 GET 请求中)。我不知道为什么它不起作用。有任何想法吗?
JS 解决方案:
$(document).ready(function(){
console.log("hello from email.js");
$('.email_citations').submit(function(event){
console.log("submit email");
var whichButton = $(".form_button[clicked=true]")[0].value
console.log("WHICH BUTTON: ");
console.log(whichButton);
var email_address = $(this).val();
var data = $(this).serialize();
data += "&" + whichButton;
console.log(data);
$.ajax({
data: data,
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data){
console.log("DATA: ");
console.log(data);
$('.email_sent_message').html(JSON.parse(data).submit_message);
}
});
return false;
});
$(".form_button").click(function() {
$(".form_button").removeAttr("clicked");
$(this).attr("clicked", "true");
});
});
html 表单:
<!-- This form works! -->
<form action="export_options" method="get">
<button type="submit" name="plain" class="button inline large hover" id="exportButton2">Download Plain Text</button>
<button type="submit" name="ris" class="button inline large hover" id="exportButton1">Download RIS File</button>
<button type="submit" name="bibtex" class="button inline large hover" id="exportButton3">Download BibTeX File</button>
</form>
<form class="email_citations" id="EmailCitationsForm" action="email_it" method="get">
<input class="input-text" type="text" size="75" maxlength="75" style="width: 40%; font-size: 16px; margin-right: 5px; height: 50px" name="email_address" placeholder="email address">
<button type="submit" value="selected" name="which_citations" class="form_button button inline large hover" id="exportButton4" >Send My Selected Citations</button>
<button type="submit" value="all" name="which_citations" class="form_button button inline large hover" id="exportButton5">Send All Citations</button>
</form>
<div class="email_sent_message" style="float: left; color: #9e1d2a; font-size: 20px; font-weight: bold">{{ submit_message }}</div>
我的 javascript/ajax 在 email.js 文件中:
$(document).ready(function(){
console.log("hello from email.js");
$('.email_citations').submit(function(event){
console.log("submit email");
var clicked_button = $(".form_button[clicked=true]")[0].name;
console.log(clicked_button);
clicked_button.push($(".form_button[clicked=true]")[0].value);
console.log(clicked_button);
var email_address = $(this).val();
var data = $(this).serialize();
data += "&" + clicked_button;
console.log(data);
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data){
$('.email_sent_message').html(JSON.parse(data).submit_message);
}
});
return false;
});
$(".form_button").click(function() {
$(".form_button").removeAttr("clicked");
$(this).attr("clicked", "true");
});
});`
我的看法:
def email_it(request):
context = RequestContext(request)
if request.method == 'GET':
getdict = request.GET.dict()
form = EmailCitationsForm(request.GET)
if form.is_valid():
context = request.session.get('All_Citations_Context')
if ('which_citations') in getdict.keys():
if 'all' in getdict.values():
text_template = 'email_allCitations.txt'
html_template = 'email_allCitations.html'
text_content = render_to_string(text_template, context, context_instance=RequestContext(request))
html_content = render_to_string(html_template, context, context_instance=RequestContext(request))
msg = EmailMultiAlternatives(subject, text_content, from_email, to)
#msg.attach()
msg.attach_alternative(html_content, "text/html")
msg.send()
submit_message = "Thank you. Your citations have been sent via email."
return http.HttpResponse(json.dumps({'submit_message': submit_message}))
if 'selected' in getdict.values():
text_template = 'email_selectedCitations.txt'
html_template = 'email_selectedCitations.html'
text_content = render_to_string(text_template, context, context_instance=RequestContext(request))
html_content = render_to_string(html_template, context, context_instance=RequestContext(request))
msg = EmailMultiAlternatives(subject, text_content, from_email, to)
msg.attach_alternative(html_content, "text/html")
msg.send()
submit_message = "Your citations have been sent via email."
return http.HttpResponse(json.dumps({'submit_message': submit_message}))
else:
submit_message = "Something went wrong. Sorry, your email did not send."
else:
submit_message = form.errors['email_address']
else:
form = EmailCitationsForm()
submit_message = ""
return http.HttpResponse(json.dumps({'submit_message': submit_message}))
好的,这更像是一个 HTML/jQuery 问题而不是 Django 问题。
首先,您的第一个表单可以正常工作,因为您可能让浏览器处理了提交。如果您使用与提供的代码类似的代码,则它不起作用。
如果你记录 $(this).serialize() 你得到的正是你在后端得到的。查看此答案 jQuery: how to get which button was clicked upon form submission?
<form class="email_citations" id="EmailCitationsForm" action="email_it" method="get">
<input class="input-text" type="text" size="75" maxlength="75" style="width: 40%; font-size: 16px; margin-right: 5px; height: 50px" name="email_address" placeholder="email address">
<button type="submit" value="selected_citations" name="selected_citations" class="form_button button inline large hover" id="exportButton4" >Send My Selected Citations</button>
<button type="submit" value="all_citations" name="all_citations" class="form_buttonbutton inline large hover" id="exportButton5">Send All Citations</button>
</form>
注意我已经删除了 django 位。
$(document).ready(function(){
console.log("hello from email.js");
$('.email_citations').submit(function(event){
console.log("submit email");
var clicked_button = $(".form_button[clicked=true]")[0].name;
console.log(clicked_button);
var email_address = $(this).val();
var data = $(this).serialize();
data += "&" + clicked_button;
console.log(data);
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data){
$('.email_sent_message').html(JSON.parse(data).submit_message);
}
});
return false;
});
$(".form_button").click(function() {
$(".form_button").removeAttr("clicked");
$(this).attr("clicked", "true");
});
});
或在 plunker 中查看 http://plnkr.co/edit/CxPGmxQfeHhtsWiANDJ1