将喜欢和页面访问计数添加到 Django 项目
Posted
技术标签:
【中文标题】将喜欢和页面访问计数添加到 Django 项目【英文标题】:Add likes and page visit counts to Django project 【发布时间】:2016-09-06 18:27:57 【问题描述】:我试图让用户能够喜欢/不喜欢对象(使用 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();
);
);
这些是我的模型,Person:
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)
【问题讨论】:
【参考方案1】:您的 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 中发送。
【讨论】:
非常感谢!但这并没有帮助,day.likes 仍然总是 0。我认为我的函数有错误,因为计数页面访问存在同样的问题,并且我在这种情况下没有使用 AJAX。【参考方案2】:问题的解决方案是在 AJAX 请求中添加$(document).ready( function())
。
【讨论】:
以上是关于将喜欢和页面访问计数添加到 Django 项目的主要内容,如果未能解决你的问题,请参考以下文章
为啥每当我尝试将图像添加到我的 django 项目时都会出错?
如何将新的 django 应用程序添加到已部署的 django 项目(使用 nginx、gunicorn)?