[英]How to run a view function containing an operation parameter without reloading page
I am creating a site which allows users to post comments and like posts. 我正在创建一个网站,允许用户发表评论和喜欢的帖子。 I have created a view function which allows a user to like posts but am not sure how to implement ajax or a similar technique to perform the request without reloading the page.
我创建了一个视图功能,允许用户喜欢帖子,但不知道如何实现ajax或类似技术来执行请求而无需重新加载页面。 So that the user doesn't lose where they were on the page and have to scroll down to find it after liking a post.
这样用户就不会失去他们在页面上的位置,并且在喜欢帖子之后必须向下滚动才能找到它。
In my views.py
: 在我的
views.py
:
#this is the function i wish to run without reloading
def like(request, operation, id):
like = Post.objects.get(id=id)
if operation == 'add':
Like.make_like(request.user, like)
elif operation == 'remove':
Like.lose_like(request.user, like)
return HttpResponseRedirect('/Feed/')
#this is the view in which it is called
class FeedView(TemplateView):
template_name = 'feed.html'
def get(self, request):
form = HomeForm()
posts = Post.objects.all().order_by('-created')
users = User.objects.exclude(id=request.user.id)
try:
like = Like.objects.get(user=request.user)
likes = like.posts.all()
except Like.DoesNotExist:
like = None
likes = None
args = {
'form': form, 'posts': posts, 'users': users, 'likes': likes
}
return render(request, self.template_name, args)
def post(self, request):
form = HomeForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.save()
text = form.cleaned_data['post']
form = HomeForm()
return HttpResponseRedirect('/Feed/')
args = {'form': form, 'text': text}
return render(request, self.template_name, args)
feed.html
: feed.html
:
{% for post in posts %}
<p class="name">{{ post.user.username }}</p>
<h3>{{ post.post }}</h3>
{% if not post in likes %}
<li-r><a class="like" href="{% url 'like' operation='add' id=post.id %}"> {{ post.total_likes }} </a></li-r>
{% endif %}
{% if post in likes %}
<li-r><a class="unlike" href="{% url 'like' operation='remove' id=post.id %}"> {{ post.total_likes }} </a></li-r>
{% endif %}
<p class="date">{{ post.created }}</p>
{% endfor %}
urls.py
: urls.py
:
urlpatterns = [
url(r'^Feed/$', FeedView.as_view(), name='feed'),
url(r'^like/(?P<operation>.+)/(?P<id>\d+)/$', views.like, name='like')
]
I have tried putting the function inside of the Feed class but I don't know how to write the url to include the class along with the "(?P<operation>.+)/(?P<id>\\d+)"
section or even if that would solve my problem. 我已经尝试将函数放在Feed类中但我不知道如何编写url以包含该类以及
"(?P<operation>.+)/(?P<id>\\d+)"
部分或即使这将解决我的问题。 But I think I can use ajax though i have no idea how. 但我想我可以使用ajax虽然我不知道如何。
You need to use Ajax here. 你需要在这里使用Ajax。 Use
onclick
event listener on the anchor tags for like/dislike, make an ajax call instead and update the count based on the response. 在锚标签上使用
onclick
事件监听器来表示喜欢/不喜欢,改为进行ajax调用并根据响应更新计数。
$('.like, .unlike').click(function(e){
e.preventDefault();
var url = $(this).attr('href'));
// make a get call on this url, and update the total_likes based on response
$.get(url, function( data ) {
$(this).text(data.total_likes); //something like that
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.