反向搜索“ edit”,未找到任何参数。尝试了1种模式:['articles / edit /(?P< pk> [0-9] +)/ $'] [英] Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['articles/edit/(?P<pk>[0-9]+)/$']

查看:43
本文介绍了反向搜索“ edit”,未找到任何参数。尝试了1种模式:['articles / edit /(?P< pk> [0-9] +)/ $']的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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]+)/$']

$ b

@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



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 %}

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个按钮正在使用主键触发编辑功能并重定向到编辑页面

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 %}

推荐答案

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

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 %}...

这样django知道您正在编辑的文章

This way django knows which article you are editing

这篇关于反向搜索“ edit”,未找到任何参数。尝试了1种模式:['articles / edit /(?P&lt; pk&gt; [0-9] +)/ $']的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆