Django视图和表单提交按钮更新数据库 [英] Django views and forms submit button update database
问题描述
这是提交按钮所在的表单。
This is the form that the submit button is in.
<form>
<button type="submit" name="subscribe" class="btn btn-primary pull-right">Subscribe</button></h5>
</form>
我在我的视图中有这个代码:
And I have this code in my view:
def tour_sub(request):
tour = Tour.objects.filter(id=1)
if 'subscribe' in request.POST:
tour.subscribers.add(user)
tour.save()
按钮被点击我只是想更新记录,并在数据库中插入。但是当我点击订阅按钮没有发生。我是django的新人,我不知道问题在哪里。
When the subscribe button is clicked I just want to update the record and insert that in the database. But nothing happens when I click the Subscribe button. I am new in django and I don't know where the problem is.
推荐答案
指定表单操作
首先,如果 tour_sub
视图不是呈现模板的视图,那么您需要指定一个表单操作形成。还可以使用输入
输入提交。
Specify Form Action
First of all you'll need to specify a the form action if the tour_sub
view is not the view that rendered the template that contains the form. Also use input
type submit.
<form action="/some/url/mapped/to/tour_sub/view/">
<input type="submit" name="subscribe" class="btn btn-primary pull-right" value="Subscribe" />
</form>
使用条件和错误处理
也可以修改你的 tour_sub
函数,并做一些错误处理,使它的方法没有无声的错误,因为他们很难调试。
Use Conditions and Error Handling
you can also modify your tour_sub
function a little and do some Error Handling so that it the method does not have silent errors as they are hard to debug.
def tour_sub(request):
tour = get_object_or_404(Tour, pk=1)
if (request.method == POST) and ("subscribe" in request.POST):
tour.subscribers.add(user)
tour.save()
# Send a Success Message to the User
else:
# Do something in case of a GET request
如果Post是AJAX
如果您使用AJAX进行POST请求,记住您将特别需要将 csrf_token
添加到发布数据。否则,您可以将以下通用 js
文件包含到您的基本模板中,它将负责将 csrf_token
附加到所有AJAX请求。
If Post is AJAX
if you are making the POST request using AJAX do remember you'll specifically need to add the csrf_token
to the POST data. Else, you can include the following generic js
file to your base template and it will take care of appending the csrf_token
to all AJAX requests.
$(document).ajaxSend(function(event, xhr, settings) {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
function sameOrigin(url) {
// url could be relative or scheme relative or absolute
var host = document.location.host; // host + port
var protocol = document.location.protocol;
var sr_origin = '//' + host;
var origin = protocol + sr_origin;
// Allow absolute or scheme relative URLs to same origin
return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
// or any other URL that isn't scheme relative or absolute i.e relative.
!(/^(\/\/|http:|https:).*/.test(url));
}
function safeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
});
希望这有助。
这篇关于Django视图和表单提交按钮更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!