使用关键字参数反转

使用关键字参数反转

我收到以下错误:



有谁知道如何解决它?

urls.py

urlpatterns = [
    url(r'^$', views.post_list, name='post_list'),
    url(r'^palan/(?P<pk>[0-9]+)/$', views.palan_detail, name='palan_detail'),
    url(r'^palan/new/$', views.plani_new, name='plani_new'),
    url(r'^nn1/new/$', views.plani_new1, name='plani_new1'),
    url(r'^palan/edit$', views.edit, name='plan_edit'),
]

views.py
def edit(request, pk):
    post = get_object_or_404(Planning, pk=pk)
    if request.method == "POST":
        form = PostFormP(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('palan_detail', pk=post.pk)
    else:
        return render(request, 'pages/plan_edit.html')

def palan_detail(request, pk):
    palan = get_object_or_404(Planning, pk=pk)
    j = Jour.objects.all().count()
    return render(request, 'pages/palan_detail.html', {'palan': palan, 'j':j},)

palan_detail.html
<a href="{% url 'plan_edit' pk=post.pk %}">
    <h1>{{ palan.parcours }}</h1>
</a>
<p>{{ palan.semestre|linebreaksbr }}</p>
<p>{{ palan.jour.jour1}}</p>

最佳答案

您的 View 中没有名为 post 的上下文变量。更改以下行

<a href="{% url 'plan_edit' pk=post.pk %}">


<a href="{% url 'plan_edit' pk=palan.pk %}">

关于python - 使用关键字参数反转 'plan_edit' '{' pk' : '' }' not found. 1 pattern(s) tried: [' palan/edit$'],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50574940/

10-12 05:42