我有一个博客,评论和类似的模型。类似的模型有一个genericforeignkey字段,所以我可以将喜欢链接到博客帖子和评论。
视图正常工作但是我在ajax请求中传递参数时遇到问题
urls.py
urlpatterns =[
url(r'^$',views.home, name = 'home'),
url(r'^blog/(?P<year>[0-9]{4})/(?P<month>[0-9]{1,2})/(?P<day>[0-9]{1,2})/(?P<slug>[-\w]+)/$',
views.blog,
name = 'view_blogpost_with_pk'),
url(r'^blog/(?P<year>[0-9]{4})/(?P<month>[0-9]{1,2})/(?P<day>[0-9]{1,2})/(?P<slug>[-\w]+)/ajax/like/$',VotesView.as_view(model=BlogPost, vote_type=Like.LIKE),
name='blog_like'),
url(r'^blog/(?P<year>[0-9]{4})/(?P<month>[0-9]{1,2})/(?P<day>[0-9]{1,2})/(?P<slug>[-\w]+)/ajax/dislike/$', VotesView.as_view(model=BlogPost, vote_type=Like.DISLIKE),
name='blog_dislike'),
blog.html
<ul>
<li data-id="{{ like_obj.id }}" data-type="blog" data-action="like" title="Like">
<span class="glyphicon glyphicon-thumbs-up"></span>
<span data-count="like">{{ like_obj.votes.likes.count }}</span>
</li>
<li data-id="{{ like_obj.id }}" data-type="blog" data-action="dislike" title="Dislike">
<span class="glyphicon glyphicon-thumbs-down"></span>
<span data-count="dislike">{{ like_obj.votes.dislikes.count }}</span>
</li>
</ul>
和javascript。只是包括不喜欢,就像非常相似
<script>
function dislike()
{
var dislike = $(this);
var type = dislike.data('type');
var pk = dislike.data('id');
var action = dislike.data('action');
var like = dislike.prev();
$.ajax({
url : '/home/blog/2017/10/1/git/ajax/dislike/',
method : 'POST',
data : { 'obj' : pk },
success : function (json) {
dislike.find("[data-count='dislike']").text(json.dislike_count);
like.find("[data-count='like']").text(json.like_count);
}
});
return false;
}
<script>
$(function() {
$('[data-action="like"]').click(like);
$('[data-action="dislike"]').click(dislike);
});
</script>
所以此刻此工作很有效,除非我用
替换urlurl: '{% url "home:blog_dislike" year=obj.year month=obj.month day=obj.date
slug=obj.slug %}'
我得到了
django.urls.exceptions.NoReverseMatch: Reverse for 'blog_like' with keyword
arguments '{'year': '', 'month': '', 'day': '', 'slug': ''}' not found. 1
pattern(s) tried: ['home/blog/(?P<year>[0-9]{4})/(?P<month>[0-9]{1,2})/(?
P<day>[0-9]{1,2})/(?P<slug>[-\\w]+)/ajax/like/$']
views.py
def blog(request,year,month,day,slug):
if request.method == 'GET':
blogpost = BlogPost.objects.get(date__year=year, date__month=month, date__day=day,slug=slug)
form = BlogPostForm()
comments = Comment.objects.filter(comment_body_id=blogpost).order_by('-date') #Link comments to blogpost // comment_body is foreignkey to blogpost model
c = comments.count()
args = {'bpost': blogpost,'form':form,'comments':comments,'c':c}
return render(request,'home/blog.html',args)
if request.method == 'POST':
blogpost = BlogPost.objects.get(date__year=year, date__month=month, date__day=day,slug=slug)
form = BlogPostForm(request.POST)
if form.is_valid():
comment = form.save(commit = False)
comment.comment_body = blogpost
comment.save()
text = form.cleaned_data['comment']
form = BlogPostForm()
return redirect(request.build_absolute_uri())
else:
form = BlogPostForm()
args = {'form':form,'text':text}
return render(request, 'home/blog.html', args)
class VotesView(View):
model = None
vote_type = None
def post(self,request, year, month, day, slug):
obj = self.model.objects.get(date__year=year, date__month=month, date__day=day,slug=slug)
try:
x = Like.objects.get(content_type=ContentType.objects.get_for_model(obj), object_id=obj.id, user=request.user)
if x.vote is not self.vote_type:
x.vote = self.vote_type
x.save(update_fields = ['vote'])
result = True
else:
x.delete()
result = False
except Like.DoesNotExist:
obj.votes.create(user=request.user, vote=self.vote_type)
result = True
return HttpResponse(
json.dumps({
"result": result,
"like_count": obj.votes.likes().count(),
"dislike_count": obj.votes.dislikes().count(),
"sum_rating": obj.votes.sum_rating()
}),
content_type="application/json"
)
感谢您的帮助