Reputation: 49
''i want to add functionality to my Publish button in HTML. i have made a method under my Post(model) and made a view for that button with a url. i dont know why my button fuctn is not working. ''
My Post Model
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
description = models.TextField()
created_on = models.DateTimeField(default=timezone.now)
published = models.BooleanField(default=False)
def publish(self):
self.published=True
self.save()
def __str__(self):
return self.title
My Views.py
@login_required
def publish_button(request,pk):
post = get_object_or_404(Post,pk)
post.publish()
return redirect('blog_detail',pk=post.pk)
my urls.py
urlpatterns = [
path('comment/<int:pk>/', comment_post, name='comment_form'),
path('publish/',PublicList.as_view(),name='publish'),
path('publish/<int:pk>/',publish_button,name='p_button'),
path('', index, name='indexpage'),
]
My html
{% extends 'base.html' %}
{% block content %}
<div class="container">
<h1>Blog Details:</h1>
<hr>
<div class="jumbotron">
<h3 align = 'center'>{{ detail.title }}</h3>
<hr>
<div class="mb-5">
<h4>{{ detail.description }}</h4>
</div>
<p>Posted By: {{ detail.user }}<span class="ml-5">{{ detail.created_on }} </span><br>
</div>
{{ detail.pk }}
<a href="{% url 'p_button' pk=detail.pk %}">Publish</a>
<p></p>
<h6>Wanna add comment? <span class="ml-3">
<a href="{% url 'comment_form' pk=detail.pk %}">
<input class="btn btn-dark" type="button" name="" value="Comment">
</a></span>
</h6>
<hr>
<h4>Comments:-</h4>
<hr>
{% for comment in detail.comments.all %}
<h5>{{comment.text}}</h5>
<p>by: @{{comment.author}}<span class='ml-5'>{{comment.commented_on}} </span></p>
<hr>
{% endfor %}
</div>
{% endblock %}
TraceBack: File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv \lib\site-packages\django\core\handlers\exception.py" in inner 34. response = get_response(request)
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\core\handlers\base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "C:\Users\AngryBuLLz\Desktop\Django\prac_18\firstapp\views.py" in publish_button
71. post = get_object_or_404(Post,pk)
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\shortcuts.py" in get_object_or_404
93. return queryset.get(*args, **kwargs)
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\db\models\query.py" in get
390. clone = self.filter(*args, **kwargs)
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\db\models\query.py" in filter
844. return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
862. clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\db\models\sql\query.py" in add_q
1263. clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\db\models\sql\query.py" in _add_q
1287. split_subq=split_subq,
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\db\models\sql\query.py" in build_filter
1161. arg, value = filter_expr
Exception Type: TypeError at /publish/5/
Exception Value: cannot unpack non-iterable int object
Upvotes: 0
Views: 61
Reputation: 599450
get_object_or_404
takes a filter expression, exactly as if you were calling Products.objects.get()`. So it should be:
post = get_object_or_404(Post, pk=pk)
Upvotes: 0