0
0 комментариев

Здравствуйте! Я добавляю в свое приложение возможность пользователям ставить/отменять лайки (с использованием AJAX), и подсчет количества посещений страницы. Для начала я пробую добавить возможность ставить лайк для объекта Day, и считать количество посещений страниц пользователей. Я создала соответствующие функции, изменила шаблоны, создала .js файл. Но В обоих случаях значение переменной не измеяется и всегда показывается 0. Подскажите, пожалуйста, где у меня ошибка? Мне кажется, что в функциях views.py.

Вот эти функции:

@login_required
def like_day(request):
day_id = None
if request.method == 'GET':
    if 'day_id' in request.GET:
        day_id = request.GET['day_id']
 
        likes = 0
        if day_id:
            day = Day.objects.get(id=int(day_id))
            if day:
                likes = day.likes + 1
                day.likes = likes
                day.save()
        return HttpResponse(likes)
 
def track_url(request):
person_id = None
url = '/friends_plans/users/'
if request.method == 'GET':
if 'person_id' in request.GET:
    person_id = request.GET['person_id']
    try:
        person = Person.objects.get(id=person_id)
        person.views = person.views + 1
        person.save()
        url = person.url
    except:
        pass
 
return redirect(url)

Это шаблоны:

<!DOCTYPE html>
{% load staticfiles %}
 
<html >
<head>
    <title> {{person.username}} </title>
    <meta charset ="utf -8" />
    <link rel="stylesheet" href="{% static 'css/style_day.css' %}">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script src="{% static 'js/friends_plans-jquery.js' %}"></script>
    <script src="{% static 'js/friends_plans-ajax.js' %}"></script>
</head>
<body>
    <div id ="container">
        <div id ="header">
            <ul id ="menu">
                <span><a href ="" >Friends' Plans</a></span>
                <span><a href ="{% url 'friends_plans:user' request.user.pk %}" >My Page</a></span>
                <span><a href ="{% url 'friends_plans:listing' %}" >Users</a></span>
                <span><a id="helpbutton" href ="" >HELP</a></span>
            </ul>
        </div>
        <div id ="left">
            <div id="border">
                <div><a class="button" href="{% url 'friends_plans:user' person.pk %}">{{person.username}}</a></div>
                <img class="cat" src={% static 'images/cat5.jpg' %} />
            </div>
            <div id="info">
                <div class ="name"> {{person.email}} </div>
                <div class ="name"> {{person.phone_number}} </div>
                <div class ="name"> Student of {{person.place_of_work_or_study}} </div>
            </div>
            <div id="empty">  </div>
 
        </div>
        <div id ="right">
            <div class="sep">
                <div class="title"> {{person}}'s plans for {{day}}: </div>
                <div class="value"> Status: {{day.level_of_business}} </div>
                {% for event in day.event_set.all %}
                <div class="title1"> <a class="button" href ="">Business: {{event.business}}</a></div>
                <div class="title1"> Type: {{event.type}}</div>
                <div class="title1"> Period of time: {{event.start_time}}-{{event.end_time}}</div> <br />
                {% endfor %}
            </div>
            <p>
                <strong id="like_count">{{ day.likes }}</strong> users like this day
                {% if user.is_authenticated %}
                <button id="likes" data-catid="{{day.id}}" class="btn btn-primary" type="button">
                    <span class="glyphicon glyphicon-thumbs-up"></span>
                    Like
                </button>
                {% endif %}
            </p>
            <div>
            {% if person.id == request.user.id %}
                <a href="{% url 'friends_plans:add_event' person.pk day.pk %}">Add event</a>
            {% endif %}
            </div>
        </div>
        <div id ="footer"> Copyright </div>
    </div>
</body>
</html>
 
{% extends 'friends_plans/base.html' %}
{% load staticfiles %}
    {% block title %} Users {% endblock %}
 
    {% block content %}
        <div id ="left">
            <div id="toptitle"> Friends' Plans members:</div>
            <table class="table">
                    <thead>
                      <tr>
                        <th>Photo</th>
                        <th>Name</th>
                        <th>Occupation</th>
                        <th>Days</th>
                        <th>Places</th>
                        <th>Wiews</th>
                      </tr>
                    </thead>
                    <tbody>
                    {% for person in users %}
                      <tr>
                        <td><span> <img class="small_cat" src={% static 'images/cat3.jpg' %} /> </span></td>
                        <td><a href="{% url 'friends_plans:user' person.pk %}">{{ person.username|upper }}</a></span></td>
                        <td><span>Student at {{ person.place_of_work_or_study}}</span></td>
                        <td>{{person.day_set.all.count}}</td>
                        <td>{{person.wish_list_set.all.count}}</td>
                        <td>{{person.wish_list.comment_to_wish_list_set.all.count}}</td>
                        <td>{% if person.views >= 0 %}
                                {{person.views}} views
                                {% elif person.views == 1 %}
                                {{person.views}} view
                            {% endif %}
                        </td>
                      </tr>
                    {% endfor %}
                    </tbody>
                  </table>
            <div class="pagination">
                <div id="listing">
                    <span class="step-links">
                        {% if users.has_previous %}
                            <a href="?page={{ users.previous_page_number }}">previous</a>
                        {% endif %}
 
                        <span class="current">
                            Page {{ users.number }} of {{ users.paginator.num_pages }}.
                        </span>
 
                        {% if users.has_next %}
                            <a href="?page={{ users.next_page_number }}">next</a>
                        {% endif %}
                    </span>
                </div>
            </div>
        </div>
    {% endblock %}

Это AJAX файл:

$('#likes').click(functin(){
var catid;
catid = $(this).attr("data-catid");
$.get('/friends_plans/like_day/', {day_id: catid}, function(data){
    $('#like_count').html(data);
    $('#likes').hide();
});
});

Это модели:

class Person (AbstractUser):
phone_number = models.CharField(max_length=30)
place_of_work_or_study = models.CharField(max_length=100)
img = models.ImageField(upload_to='photos/', null=True, blank=True)
url = models.URLField(null=True, blank=True)
views = models.IntegerField(default=0)
class Meta:
    verbose_name = 'Person'
    verbose_name_plural = 'Users'
def __unicode__(self):
    return self.username
 
 
class Day(models.Model):
person = models.ManyToManyField(Person)
date = models.DateField()
url = models.URLField(null=True, blank=True)
views = models.IntegerField(default=0)
likes = models.IntegerField(default=0)
levels = (
    ('busy', 'busy'),
    ('has_suggestions', 'has_suggestions'),
    ('waiting_for_suggestions', 'waiting_for_suggestions')
)
level_of_business = models.CharField(choices=levels, max_length=40, default='waiting_for_suggestions')
def __unicode__(self):
    return unicode(self.date)


Добавить комментарий