我试图让用户能够喜欢/不喜欢对象(使用ajax),并计算页面访问次数。我已经编写了函数并更改了模板,但某处出现了错误。喜欢和观点的结果总是为0.在我看来,错误在于views.py函数,但也许我错了,我是Django的初学者。现在根据功能,允许用户多次喜欢一个对象。我想让用户不要这样做,但要提供不喜欢该对象的能力。但首先我想要至少一个简单的工作正常。首先,我尝试计算用户页面的访问次数,并为模型日添加喜欢。 你能告诉我如何更正我的文件吗?
这是观点的一部分:
@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)
这是list.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 %}
这是day.html(那里有一个类似的按钮)
<!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>
这是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)
答案 0 :(得分:0)
您的AJAX代码的语法不正确。它应该是
$('#likes').click(function(){
var catid;
catid = $(this).attr("data-catid");
$.get('/friends_plans/like_day/', {day_id: catid}, function(data){
$('#like_count').html(data);
$('#likes').hide();
});
});
此外,请注意您应不使用GET
来操纵服务器上的数据。这可能被CSRF attacks滥用。请改用POST
如果您选择使用POST
,则不应忘记在模板中生成csrf-token,并将其与请求一起作为X-Header或cookie内部发送。
答案 1 :(得分:0)
问题的解决方案是将$(document).ready( function(){})
添加到AJAX请求中。