''我想向HTML中的“发布”按钮添加功能。我已经在我的Post(模型)下创建了一个方法,并使用url对该按钮进行了查看。我不知道为什么我的按钮功能不起作用。
我的帖子模型
PUBLIC
我的Views.py
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
我的urls.py
@login_required
def publish_button(request,pk):
post = get_object_or_404(Post,pk)
post.publish()
return redirect('blog_detail',pk=post.pk)
我的html
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'),
]
TraceBack: 内部文件“ C:\ Users \ AngryBuLLz \ AppData \ Local \ conda \ conda \ envs \ madeenv \ lib \ site-packages \ django \ core \ handlers \ exception.py” 34. response = get_response(request)
{% 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 %}
126。响应= 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。响应= wraped_callback(请求,* callback_args,** callback_kwargs)
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\core\handlers\base.py" in _get_response
21。返回view_func(request,* args,** kwargs)
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
71。发布= get_object_or_404(Post,pk)
File "C:\Users\AngryBuLLz\Desktop\Django\prac_18\firstapp\views.py" in publish_button
93。返回queryset.get(* args,** kwargs)
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\shortcuts.py" in get_object_or_404
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 get
844。返回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
862。 clone.query.add_q(Q(* args,** kwargs))
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
1263。子句,_ = 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 _add_q
1161。 arg,值= filter_expr
File "C:\Users\AngryBuLLz\AppData\Local\conda\conda\envs\madeenv\lib\site-packages\django\db\models\sql\query.py" in build_filter
答案 0 :(得分:0)
get_object_or_404
接受一个过滤器表达式,就像您在调用Products.objects.get()`一样。所以应该是:
post = get_object_or_404(Post, pk=pk)