在Django中没有反向匹配

2024-10-16 20:42:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直面临着这个问题。我有一个url名称张贴页面的详细信息,但也得到了错误,请 请参见下面的错误屏幕截图。 error screen shot

我的html页面

<a href="{% url "post-detail-page" slug=post.slug %}">
    <h2>{{post.tractor_company}} || {{post.tractor_model}}</h2>
<pre><h5>{{post.implimentaion}}</h5>
{{post.farmer}}
Uplode Date : {{post.date}}</pre>
</a>
</div>

url.py

from . import views
urlpatterns = [
    path("",views.starting_page,name = "starting-page"),
    path("posts",views.posts,name = "post-page"),
    path("posts/<slug:slug>",views.post_detail,name="post-detail-page"),
]

View.py

from django import forms
from django.contrib.auth.models import User
from django.shortcuts import render, redirect ,get_object_or_404
from .models import Post, Farmer
# Create your views here.
from django.http import HttpResponseRedirect 
# Create your views here.
def starting_page(request):
    return render(request,"tractor/index.html")

def posts(request):
    qs = Post.objects.all()  
    context = {"posts":qs}
    return render(request,"tractor/all-post.html",context)
    
def add_post(request):
    pass


def post_detail(request,slug):
    indentified_post = get_object_or_404(Post,slug=slug)
    return render(request,"blog/post-detail.html",{'post':indentified_post})

我正在反复浏览帖子并使用post-detail.html页面

all-post.html

{% load static %}

{% block title %}
All Tractors
{% endblock  %}

{% block content%}
<section id="all_events">
    <br>
    <h1 style="text-align:center;">All Tractors</h1>
    <ul>
      {% for post in posts %}
        <br>
        {% include "tractor/includes/singlePost.html" %}
      {% endfor %}
    </ul>

</section>
{% endblock %}```

Tags: djangofromimportrequestdefhtmlpageall