我认为这是一个非常简单的问题,但我不知道出什么问题了。 我保留了错误。
它说:
异常类型:NoReverseMatch
异常值:带有关键字参数的“ post_publish”的取反 找不到'{'pk':''}'。尝试了1种模式: ['\ ^ post / \(\?P(?P [^ /] +)\\ d \ + \)/ publish / \ $$']
例外 位置:C:\ Users \ umts \ lib \ site-packages \ django \ urls \ resolvers.py _reverse_with_prefix,第622行
你们可以帮助我解决这些问题吗?
urls.py
urlpatterns = [
path('', views.post_list, name='post_list'),
path('post/<int:pk>/', views.post_detail, name='post_detail'),
path('post/new', views.post_new, name='post_new'),
path('post/<int:pk>/edit/', views.post_edit, name='post_edit'),
path(r'^drafts/$', views.post_draft_list, name='post_draft_list'),
path(r'^post/(?P<pk>\d+)/publish/$', views.post_publish, name='post_publish'),
path(r'^post/(?P<pk>\d+)/remove/$', views.post_remove, name='post_remove'),
path(r'^post/(?P<pk>\d+)/comment/$', views.comment_add, name='comment_add'),
path(r'^comment/(?P<pk>\d+)/approve/$', views.comment_approve, name='comment_approve'),
path(r'^comment/(?P<pk>\d+)/remove/$', views.comment_remove, name='comment_remove'),
]
我将仅显示“注释添加”代码
views.py
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})
# comment 더하기
def comment_add(request, pk):
post=get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.post = post
comment.save()
return redirect('post_detail', pk=post.pk)
else:
form=CommentForm()
return render(request, 'blog/post_detail.html') #, {'form':form})
post_detail.html
{% extends 'blog/base.html' %}
{% block content %}
<div class="post">
{% if post.published_date %}
<div class="date">
{{ post.published_date }}
</div>
{% else %}
<!--버튼은 디폴트값, 기능은 post_publish-->
<a class="btn btn-default" href="{% url 'post_publish' pk=post.pk %}">
Publish
</a>
<!--버튼은 디폴트값, 기능은 post_edit-->
<a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}">
<span class="glyphicon glyphicon-pencil">
</span>
</a>
<!--버튼은 디폴트값, 기능은 post_remove-->
<a class='btn btn-default' href="{% url 'post_remove' pk=post.pk %}">
<span class="glyphicon glyphicon-remove">
</span>
</a>
{% endif %}
<h1>{{ post.title }}</h1>
<p>{{ post.text|linebreaksbr }}</p>
</div>
<!--Comment 입력창-->
<hr>
<form action="{% url 'comment_add' pk=post.pk %}" method="POST" class="comment-form">
{% csrf_token %}
<input type="text" name="text" placeholder="댓글 달기...">
</form>
<!--
<form class="post-form" method="POST">
{% csrf_token %}
<!--{{ form.as_p }}
<button type="submit" class="save btn btn-default">
Add comment
</button>
<input type="text" name="content" value="Add Comment">
</form>-->
<!-- 코멘트 보기 -->
<hr>
{% for comment in post.comments.all %}
{% if user.is_authenticated or comment.approved_comment %}
<div class="comment">
<div class="date">
{{ comment.created_date }}
{% if not comment.approved_comment %}
<a class="btn btn-default" href="{% url 'comment_remove' pk=comment.pk %}"><span class="glyphicon glyphicon-remove"></span></a>
<a class="btn btn-default" href="{% url 'comment_approve' pk=comment.pk %}"><span class="glyphicon glyphicon-ok"></span></a>
{% endif %}
</div>
{{ comment.author }}
<p>{{ comment.text|linebreaks}}</p>
</div>
{% endif %}
{% empty %}
<p>No Comments here yet</p>
{% endfor %}
{% endblock %}
您可以忽略韩语单词。但是,我要添加注释。