我试图制作一个简单的按钮,在数据库中将我的值增加1并在页面上显示。
我在Django - How to increment integer field from user input?找到了一些内容,但它并没有解决问题。
我在views.py中的代码:
if request.method == 'POST':
id = request.POST.get('slug')
vote = request.POST.get('voo')
glosy = request.POST.get('glosy')
git = Photo.objects.all().filter(pk=id, votes = vote)
vote = int(vote)+1
p = Photo.objects.get(pk=id)
print p.votes3
p.votes3 += 1
print p.votes3
p.save()
Photo.objects.all().filter(pk=id).update(votes3=10)
和我在模板中的代码:
{% extends "photologue/root.html" %}
{% load photologue_tags i18n %}
{% load comments %}
{% block title %}{{ obj.title }}{% endblock %}
{% block content %}
{% load static %}
<script src="{% static 'js/pinol-ajax.js' %}"></script>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '876940702346526',
xfbml : true,
version : 'v2.2'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div class="row col-lg-12">
<h1 class="page-header">{{ obj.title }}</h1>
<p class="muted"><small>{% trans "Published" %} {{ obj.date_added }}</small></p>
</div>
<div class="row">
<div class="col-md-6">
{% if obj.caption %}<p>{{ obj.caption|safe }}</p>{% endif %}
<a href="{{pho.image.url}}" data-lightbox="roadtrip" alt = "">
<img src="{{ pho.get_display_url }}" class="thumbnail" alt="{{ obj.title }}"></a>
<br>
<!-- <button id="likes" data-catid="{{obj.title}}" class="btn btn-primary" type="button"> Like </button> {{ pho.votes }} {{ object.view_count }} -->
<strong id="like_count">{{ pho.votes }}</strong> people like this photo
{% if user.is_authenticated %}
<form action="" method="post">
{% csrf_token %}
<button type="submit" value="preview" name="Preview">HUEHUE</button>
{% endif %}
<div class="fb-share-button" data-layout="button_count"></div>
</a>
</div>
<div class="col-md-6">
{% if pho.public_galleries %}
<p>{% trans "This photo is found in the following galleries" %}:</p>
<table>
{% for gallery in pho.public_galleries %}
<tr>
<td>{% previous_in_gallery pho gallery %}</td>
<td class="text-center"><a href="">{{ gallery.title }}</a></td>
<td>{% next_in_gallery pho gallery %}</td>
</tr>
{% endfor %}
</table>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}fluent_comments/css/ajaxcomments.css" />
<script type="text/javascript" src="{{ STATIC_URL }}fluent_comments/js/ajaxcomments.js"></script>
<div class="fb-like" data-href="{{request.META.HTTP_HOST}}" data-layout="standard" data-action="like" data-show-faces="true" data-share="true"></div>
<div class="fb-comments" data-href="{{request.META.HTTP_HOST}}" data-numposts="5" data-colorscheme="light"></div>
<!-- {{request.META.HTTP_HOST}}{{request.get_full_path}} -->
{% render_comment_list for pho %}
{% if user.is_authenticated %}
{% render_comment_form for pho %}
{% else %}
Zaloguj się aby dodawać komentarze! :)
{% endif %}
{% endif %}
{% endblock %}
答案 0 :(得分:1)
你看到什么错误?
此外,您用于过滤的slug2
变量的值是多少?这应该通过帖子传递,所以你需要在你的表单中添加一个隐藏的输入,它将传递slug2的值......就像这样:
<input type='hidden' value='{{ slug2 }}' name='slug2' />
然后在您的视图中获取POST
字典中的值:
slug2 = request.POST.get('slug2')
更新(在看到OP的代码之后):您需要将slug
传递给您的表单操作。所以,使用这样的东西:
{% if user.is_authenticated %}
<form action="" method="post">
{% csrf_token %}
<button type="submit" value="preview" name="Preview">HUEHUE</button>
</form>
{% endif %}
不会工作,因为您只需再次访问调用视图(空操作POSTS到当前URL - 操作参数需要有值)。
我不知道您的Photo_det
视图的网址名称,因此我们假设它被称为photo_det
(这在urls.py
中定义)。你的表格应该是这样的
<form action="{% url 'photo_det' photo.slug %}" method="post">
以使表单操作成为Photo_det
函数视图。