"Like" counter, increment value by click in django -
im trying make simple button increament value 1 in database , show on page.
i found on django - how increment integer field user input? 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 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"> </button> {{ pho.votes }} {{ object.view_count }} --> <strong id="like_count">{{ pho.votes }}</strong> people 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 found in following galleries" %}:</p> <table> {% 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 pho %} {% if user.is_authenticated %} {% render_comment_form pho %} {% else %} zaloguj się aby dodawać komentarze! :) {% endif %} {% endif %} {% endblock %}
what error see ?
also, value of slug2
variable use filter ? should passed through post, need add hidden input in form pass value of slug2... this:
<input type='hidden' value='{{ slug2 }}' name='slug2' />
and in view value post
dictionary:
slug2 = request.post.get('slug2')
update (after seeing op's code): need pass slug
form action . so, using 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 visit again calling view (an empty action posts current url -- action parameter needs have value).
i don't know url name of photo_det
view, let's called photo_det
(this defined in urls.py
). form should
<form action="{% url 'photo_det' photo.slug %}" method="post">
in order form action photo_det
function view.
Comments
Post a Comment