python - 对 'edit' 进行反向操作,未找到参数 '(9,)' 和关键字参数 '{}'。尝试了 0 个模式 : []

标签 python django django-models django-templates django-views

我在尝试访问 django 中的编辑链接时遇到错误,我在此处查看了堆栈溢出,但尚未找到适合我的情况的解决方案。

错误: 异常类型:NoReverseMatch 异常值:与参数“(9,)”和关键字参数“{}”相反的“编辑”未找到。尝试了 0 个模式:未找到 [] 参数“{}”。尝试了 0 个模式:[]

这是我的 urls.py

from django.conf.urls import url, include
from django.contrib import admin
from posts import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^create/$', views.create, name='create'),
    url(r'^(?P<id>\d+)/$', views.show_post, name = 'show_post'),
    url(r'^(?P<id>\d+)/edit/$', views.update_post, name = 'update_post'),
    url(r'^(?P<id>\d+)/delete/$', views.delete_post),
]

View .py

from django.contrib import messages
from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse, HttpResponseRedirect

from .forms import PostForm
from .models import Post

# Create your views here.

def index(request):
    post_list = Post.objects.order_by('-created_date')[:10]
    context = {'post_list': post_list}
    return render(request, 'index.html', context)

def create(request):
    form = PostForm(request.POST or None)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        #flass messages
        messages.success(request, "Successfully created")
        return HttpResponseRedirect(instance.get_absolute_url())


    context = {
        "form":form,
    }
    return render(request, 'post_form.html', context)   


def show_post(request, id=None):
    instance = get_object_or_404(Post, id=id)
    context = {'instance': instance}
    return render(request, 'show_post.html', context)


def update_post(request, id=None):
    instance = get_object_or_404(Post, id=id)
    form = PostForm(request.POST or None, instance=instance)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        messages.success(request, "Post updated")
        return HttpResponseRedirect(instance.get_absolute_url())
    context = {
        "form":form,
        "instance":instance
    }
    return render(request, 'post_form.html', context)


def delete_post(request, id=None):
    instance = get_object_or_404(Post, id=id)
    instance.delete()
    messages.success(request, "Successfully deleted")
    return redirect("posts:index")

show_post.html

{% extends "base.html" %}

<div class="container">
{% block content  %}
    <h1> {{instance.title}} </h1>
    <h3>{{instance.content| linebreaks}} </h3>
    <a href="{% url 'posts:index' %}"> Home</a> | <a href="{{instance.url}}" target="_blank" > visit url</a> | 
    <a href="{% url 'posts:update_post' %}"> Edit</a> 

{% endblock %}

</div>

最佳答案

您需要使用命名空间。您应该使用 'posts:edit',而不是 'edit'

'posts:update_post',具体取决于您在 urls.py 中使用的名称。

关于python - 对 'edit' 进行反向操作,未找到参数 '(9,)' 和关键字参数 '{}'。尝试了 0 个模式 : [],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39601272/

相关文章:

Python:双重排序

python - Tkinter:斜体已经粗体的文本(重叠标签)

django - 使用 post_save 信号处理程序访问新创建的模型实例的相关数据

python - Django 在多对多关系模型中轻松检索项目

python - 使用 unittest 为 Python 中的许多测试用例提供正确的结构

python - 设置自定义轴值 pyplot

python - 附加 Django HTTP 代理 URL

javascript - JSON.parse 仅在部署在 pythonanywhere 上时返回语法错误

python - 当没有空字段时,该字段为必填字段,出现错误

python - Django 模型,InnoDB 中的表