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

℡╲_俬逩灬. 提交于 2019-12-24 01:36:18

问题


I am a beginner in Django and now i am developing a blogging application. At the article editing section i got stucked and I dont know why its showing this error. Searched a lot and cant find an answer

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 section in

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

That button in 4th is triggering the edit function with primary key and redirects to edit page

<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:


In your template article_edit.html - post url is expecting a pk, you need to pass a pk just like you are doing it for template my_articles.html

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

This way django knows which article you are editing



来源:https://stackoverflow.com/questions/52129536/reverse-for-edit-with-no-arguments-not-found-1-patterns-tried-articles-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!