I am new to django and I want to make a like button on posts but I have this error. Thank you!
'WSGIRequest' object has no attribute 'Post'
This is my Post model:
class Post(models.Model):
created_date = models.DateTimeField()
title = models.CharField(max_length=100)
profile_image = models.ImageField(upload_to='poze', blank=True, null=True)
text = models.CharField(max_length=1000, default='Nimic', blank=True)
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
likes=models.ManyToManyField(UserProfile,related_name='likes',blank=True )
This is my html template:
{% extends 'base2.html' %}
{% load static %}
{% load rest_framework %}
{% load crispy_forms_tags %}
{% block content %}
{% for post in posts %}
<form method="post" class="form-signin">
<div class="card text-center " style="width: 30rem;">
<img class="card-img-top" src='' alt="Card image cap">
<div class="card-body">
<h5 class="card-title">{{post.title}}</h5>
<h6 class="card-subtitle mb-2 text-muted">{{ post.created_date}}</h6>
<p class="card-text">{{post.text}}</p>
<div class="card-footer text-muted">
<a href="{% url 'comments' pk=post.id %}" class="card-link">Comments</a>
<!--<a href="{% url 'like_post' %}" class="card-link" value="{{ post.id }}" name="post_id">Like</a>-->
</div>
</div>
</div>
</form>
<form action="{% url 'like_post' %}" method="post">
{% csrf_token %}
<button type="submit" name="post_id" value="{{ post.id}}">Like</button> //HERE IS THE LIKE BUTTON
</form>
{% endfor %}
{% endblock %}
URL:
url(r'^like/', login_required(views.LikePost), name='like_post'),
VIEW:
def LikePost(request):
post=get_object_or_404(Post,id=request.Post.get('post_id'))
post.likes.add(request.user)
return HttpResponseRedirect(post.get_absolute_url())
Console:
System check identified no issues (0 silenced). August 12, 2018 - 16:28:05 Django version 2.0.7, using settings 'DjangoApp.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK. Internal Server Error: /account/like/ Traceback (most recent call last): File "C:\Users\Robbi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py", line 35, in inner response = get_response(request) File "C:\Users\Robbi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\Robbi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\Robbi\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view return view_func(request, *args, **kwargs) File "C:\Users\Robbi\PycharmProjects\DjangoApp\account\views.py", line 198, in LikePost post=get_object_or_404(Post,id=request.Post.get('post_id')) AttributeError: 'WSGIRequest' object has no attribute 'Post' [12/Aug/2018 16:46:10] "POST /account/like/ HTTP/1.1" 500 72571