Reputation: 33
Im trying to make simple button that will increament my value by 1 in database and show it on page.
I found something on Django - How to increment integer field from user input? but it doesn't solve problem.
My code in 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)
and my code in template:
{% 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 %}
Upvotes: 1
Views: 3302
Reputation: 15104
What error do you see ?
Also, what is the value of the slug2
variable you use to filter ? This should be passed through the post, so you need to add a hidden input in your form that will pass the value of the slug2... Something like this:
<input type='hidden' value='{{ slug2 }}' name='slug2' />
And then in your view get the value from the POST
dictionary:
slug2 = request.POST.get('slug2')
Update (after seeing OP's code): You need to pass the slug
to your form action . So, using something like this:
{% if user.is_authenticated %}
<form action="" method="post">
{% csrf_token %}
<button type="submit" value="preview" name="Preview">HUEHUE</button>
</form>
{% endif %}
Will not work since you'll just visit again the calling view (an empty action POSTS to the current URL -- the action parameter needs to have a value).
I don't know the url name of your Photo_det
view, so let's say that it is called photo_det
(this is defined in urls.py
). Your form should be something like this
<form action="{% url 'photo_det' photo.slug %}" method="post">
in order for the form action to be the Photo_det
function view.
Upvotes: 1