Я создал простое приложение для статьи / блога. У меня проблема в подробном представлении. Я создал вид списка статей, а также детальный вид, если кто-то нажимает на одну из статей, она переходит к подробному виду статьи.
Но когда я нажимаю на одну из статей, я получаю вышеуказанную ошибку, указанную в заголовке. Это также указывает на место ошибки
этот код {% url "detail" post.id%}, который вы можете увидеть в 44-й или 45-й строке на странице ошибок, которую я разместил в последней.
Я много чего пробовал, ничего не получалось.
Я разместил все мои файлы проекта ниже. Вы можете посмотреть.
Я был бы рад, если бы вы, ребята, помогли мне.
Мой код идет сюда:
Views.py
from django.shortcuts import render , redirect , get_object_or_404
from django.shortcuts import render , redirect , get_object_or_404
from .models import Article , members
from django.views.generic import ListView , DetailView
from .forms import create_form
class article_view(ListView):
model = Article
template_name = "article.html"
context_object_name = "articles"
def post_creator(request):
if request.method == "POST":
form = create_form(request.POST)
if form.is_valid():
form.save()
return redirect("/blog/home/")
else:
form = create_form()
return render(request , "post_create.html" , {"form":form})
def registration(request):
if request.method == "POST":
form = members(request.POST)
if form.is_valid():
form.save()
return redirect("/blog/home/")
else:
form = members()
return render(request , "register.html" , {"form":form})
class post_detail_view(DetailView):
model = Article
template_name = "detail.html"
urls.py
from django.conf.urls import url
from . import views
from django.contrib.auth.views import LoginView , LogoutView
urlpatterns = [
url("home/" , views.article_view.as_view() , name="blog-home"),
url("create/" , views.post_creator , name="new_post"),
url("register/" , views.registration , name="register"),
url("login/" , LoginView.as_view(template_name="login.html") , name="login"),
url("logout/" , LogoutView.as_view(template_name="logout.html") , name="logout"),
url("article/<int:pk>/" , views.post_detail_view.as_view() , name="detail"),
]
article.html (вы можете принять это как шаблон base.html)
{% load static %}
<!DOCTYPE html>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>Home</title>
</head>
<body style="background-image: url('{% static 'wooden.jpg' %}');">
{% if user.is_authenticated %}
<fieldset style="position: absolute;top: 20px;right: 0px;width: 100px;">
<button class="btn btn-outline-info"; onclick="location.href='{% url 'logout' %}'">Logout</button>
</fieldset>
<fieldset style="position: absolute;top: 20px;left: 20px;width: 100px;">
<button class="btn btn-outline-info">{{ user.username }}</button>
</fieldset>
<fieldset style="position: absolute;top: 20px;right: 120px;width: 100px;">
<button class="btn btn-outline-info" onclick="location.href='{% url 'new_post' %}'">Create Post</button>
</fieldset>
{% else %}
<fieldset style="position: absolute;top: 20px;right: 0px;width: 100px;">
<button class="btn btn-outline-info" onclick="location.href='{% url 'login' %}'">Login</button>
</fieldset>
<fieldset style="position: absolute;top: 20px;right: 80px;width: 100px;">
<button class="btn btn-outline-info" onclick="location.href='{% url 'register' %}'">Signup</button>
</fieldset>
{% endif %}
{% for post in articles %}
<div>
<fieldset style="width:45%;padding: 15px ;background-color: white;margin-left: 150px;margin-bottom: 10px;margin-top: 20px;border: 3px solid #20B2AA;opacity: 0.7; ">
<h1><a href="{% url 'detail' post.id %}">{{ post.title }}</a></h1>
<p>Click to see detail Post !</p>
<small>Published by {{ post.author }} on {{ post.date_pub }}</small></br></br>
</fieldset>
</div>
{% endfor %}
</body>
</html>
detail.html
{% load static %}
<!DOCTYPE html>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>Home</title>
</head>
<body style="background-image: url('{% static 'wooden.jpg' %}');">
{% if user.is_authenticated %}
<fieldset style="position: absolute;top: 20px;right: 0px;width: 100px;">
<button class="btn btn-outline-info"; onclick="location.href='{% url 'logout' %}'">Logout</button>
</fieldset>
<fieldset style="position: absolute;top: 20px;left: 20px;width: 100px;">
<button class="btn btn-outline-info">{{ user.username }}</button>
</fieldset>
<fieldset style="position: absolute;top: 20px;right: 120px;width: 100px;">
<button class="btn btn-outline-info" onclick="location.href='{% url 'new_post' %}'">Create Post</button>
</fieldset>
{% else %}
<fieldset style="position: absolute;top: 20px;right: 0px;width: 100px;">
<button class="btn btn-outline-info" onclick="location.href='{% url 'login' %}'">Login</button>
</fieldset>
<fieldset style="position: absolute;top: 20px;right: 80px;width: 100px;">
<button class="btn btn-outline-info" onclick="location.href='{% url 'register' %}'">Signup</button>
</fieldset>
{% endif %}
<div>
<fieldset style="width:45%;padding: 15px ;background-color: white;margin-left: 150px;margin-bottom: 10px;margin-top: 20px;border: 3px solid #20B2AA;opacity: 0.7; ">
<h1>{{ object.title }}</h1>
</fieldset>
<fieldset style="width:45%;padding: 15px ;background-color: white;margin-left: 150px;margin-bottom: 10px;margin-top: 20px;border: 3px solid #20B2AA;opacity: 0.7; ">
<p>{{ object.content }}</p>
</fieldset>
<fieldset style="width:45%;padding: 15px ;background-color: white;margin-left: 150px;margin-bottom: 10px;margin-top: 20px;border: 3px solid #20B2AA;opacity: 0.7; ">
<small>Published by {{ object.author }} on {{ object.date_pub }}</small></br></br>
</fieldset>
</div>
</body>
</html>
Ошибка страницы
NoReverseMatch at /blog/home/
Reverse for 'detail' with arguments '(1,)' not found. 1 pattern(s) tried: ['blog/article/<int:pk>/']
Request Method:
GET
Request URL:
http://127.0.0.1:8000/blog/home/
Django Version:
2.1
Exception Type:
NoReverseMatch
Exception Value:
Reverse for 'detail' with arguments '(1,)' not found. 1 pattern(s) tried: ['blog/article/<int:pk>/']
Exception Location:
C:\Users\nitin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 622
Python Executable:
C:\Users\nitin\AppData\Local\Programs\Python\Python37-32\python.exe
Python Version:
3.7.0
Python Path:
['C:\\Users\\nitin\\Desktop\\my_project',
'C:\\Users\\nitin\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
'C:\\Users\\nitin\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
'C:\\Users\\nitin\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
'C:\\Users\\nitin\\AppData\\Local\\Programs\\Python\\Python37-32',
'C:\\Users\\nitin\\AppData\\Roaming\\Python\\Python37\\site-packages',
'C:\\Users\\nitin\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages']
Server time:
Mon, 5 Nov 2018 07:32:43 +0000
Error during template rendering
In template C:\Users\nitin\Desktop\my_project\blog\templates\article.html, error at line 44
Reverse for 'detail' with arguments '(1,)' not found. 1 pattern(s) tried: ['blog/article/<int:pk>/']
34
</fieldset>
35
<fieldset style="position: absolute;top: 20px;right: 80px;width: 100px;">
36
<button class="btn btn-outline-info" onclick="location.href='{% url 'register' %}'">Signup</button>
37
</fieldset>
38
{% endif %}
39
40
{% for post in articles %}
41
<div>
42
43
<fieldset style="width:45%;padding: 15px ;background-color: white;margin-left: 150px;margin-bottom: 10px;margin-top: 20px;border: 3px solid #20B2AA;opacity: 0.7; ">
44
<h1><a href="{% url 'detail' post.id %}">{{ post.title }}</a></h1>
45
<p>Click to see detail Post !</p>
46
<small>Published by {{ post.author }} on {{ post.date_pub }}</small></br></br>
47
</fieldset>
48
</div>
49
50
{% endfor %}
51
52
</body>
53
</html>