反向搜索“ edit”,未找到任何参数。尝试了1种模式:['articles / edit /(?P <pk> [0-9] +)/ $']

时间:2018-09-01 15:24:20

标签: django python-3.x django-forms

我是Django的初学者,现在我正在开发博客应用程序。在文章编辑部分,我被卡住了,我不知道为什么它显示此错误。搜索了很多,却找不到答案

NoReverseMatch at /articles/edit/2/

Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['articles/edit/(?P<pk>[0-9]+)/$']

中的

edit_articles部分

views.py

@login_required(login_url="/accounts/login/")
def edit_articles(request, pk):
    article = get_object_or_404(Article, id=pk)
    if request.method == 'POST':
        form = forms.CreateArticle(request.POST, instance=article)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            return redirect('articles:myarticles')
    else:
        form = forms.CreateArticle(instance=article)
    return render(request, 'articles/article_edit.html', {'form': form})

article_edit.html

{% extends 'base_layout.html' %}

{% block content %}
<div class="create-article">
  <form class="site-form" action="{% url 'articles:edit' %}" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    <div class="jumbotron">
      <div class="heading col-md-12 text-center">
        <h1 class="b-heading">Edit Article</h1>
      </div>
      <div class="row">
        <div class="col-md-6">
          <div class="form-group">
            {{ form.title }}
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group">
            {{ form.slug }}
          </div>
        </div>
        <div class="col-md-6">
          <div class="form-group">
            {{ form.thumb }}
          </div>
        </div>
        <div class="col-md-12">
          <div class="form-group">
            {{ form.body }}
          </div>
        </div>
      </div>
      <button type="submit" class="btn btn-primary btn-lg btn-block">Update</button>
    </div>
  </form>
</div>

{% endblock %}

urls.py

from django.urls import path
from . import views

app_name = 'articles'

urlpatterns = [
    path('', views.article_list, name='list'),
    path('create/', views.article_create, name='create'),
    path('edit/<int:pk>/', views.edit_articles, name='edit'),
    path('myarticles/',views.my_articles, name='myarticles'),
    path('<slug>/', views.article_detail, name='details'),
]

my_articles.html

第4个按钮使用主键触发编辑功能,并重定向到编辑页面

<tbody>
{% if articles %}
{% for article in articles %}
<tr class="table-active">
  <th scope="row">1</th>
  <td>{{ article.title }}</td>
  <td>{{ article.date }}</td>
  <td><a href="{% url 'articles:edit' pk=article.pk %}"><button type="button" class="btn btn-info">Edit</button></a></td>
{% endfor %}
</tr>
{% else %}
<tr>
  <td colspan=4 class="text-center text-danger">Oops!! You dont have any articles.</td>
</tr>
{% endif %}

1 个答案:

答案 0 :(得分:3)

在您的模板article_edit.html中-发布网址需要一个pk,您需要传递一个pk,就像对my_articles.html模板所做的一样

<div class="create-article"><form class="site-form" action="{% url 'articles:edit' pk={{object.pk}} %}" method="post" enctype="multipart/form-data">{% csrf_token %}...

django通过这种方式知道您正在编辑的文章