2014-12-16 2 views
0

Im은 데이터베이스에서 내 값을 1 씩 증가시켜 페이지에 표시하는 간단한 버튼을 만들려고합니다."Like"카운터, 장고를 클릭하여 값을 증가 시키십시오.

나는 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 %} 

답변

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 %}  

방금 ​​호출보기를 다시 방문 할 것이기 때문에하지 일 (현재 URL에 빈 액션 게시물이 - 액션 매개 변수에 값이 필요).

Photo_det보기의 URL 이름을 모르므로 photo_det (이 내용은 urls.py에 정의되어 있음)이라고 가정 해 보겠습니다. 양식 조치가 Photo_det 기능보기로에 대한 귀하의 양식을 위해이

<form action="{% url 'photo_det' photo.slug %}" method="post"> 

과 같이해야한다.

+0

페이지가 새로 고쳐 지지만 페이지의 카운터가 증가하지 않습니다. 귀하의 솔루션을 점검하고 어떤 일이 발생했는지 알려주십시오. – bMh

+0

작동하지 않는 경우 전체 템플릿 및보기를 제공하십시오. – Serafeim

+0

양식 동작이 변경된 후 오류가 발생했습니다 : " '(' ',)'및 '{' '인수가있는'photo_det '에 대해 역방향으로 실행했습니다.} 0 패턴 시도 : []" – bMh

관련 문제