Я получаю ошибку No reverse match в django, но используется urls.py
Я столкнулся с этой проблемой. У меня есть url имя post-page-detail, но затем также получаю ошибку, пожалуйста.
Смотрите скриншот ошибки ниже.
Моя html-страница singlepost.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>
URLs.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 %}```
будем считать, что "tractor/includes/singlePost.html" это
<div class="card shadow1">
<a href="{% url 'post-detail-page' post.slug %}">
<h2>{{post.tractor_company}} || {{post.tractor_model}}</h2>
<pre><h5>{{post.implimentaion}}</h5>
{{post.farmer}}
Uplode Date : {{post.date}}</pre>
</a>
</div>
теперь внутри файла 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" with post=post %}
{% endfor %}
</ul>
</section>
{% endblock %}
проблема заключается в том, что когда вы вызываете это
{% include "tractor/includes/singlePost.html" %}
django не может понять, что такое пост внутри singlePost.html, поэтому, чтобы дать django понять, что вы должны передать пост как параметр, что-то вроде этого
{% include "tractor/includes/singlePost.html" with post=post %}
Вместо "{% url "post-detail-page" slug=post.slug %}" используйте следующее:
"{% url 'post-detail-page' post.slug %}"
попробуйте это: "{% url 'insert-app-name-here:post-detail-page' %}"