对 'detail' 进行反转,未找到参数“(1,)”。尝试了 1 种模式:['blog/article/<int:pk>/']
Reverse for 'detail' with arguments '(1,)' not found. 1 pattern(s) tried: ['blog/article/<int:pk>/']
我创建了一个简单的 article/blog 应用程序。我在详细视图中遇到问题。我创建了一个文章列表视图,还创建了一个详细视图,如果有人点击其中一篇文章,它会显示文章的详细视图。
但是当我点击其中一篇文章时,出现了上述标题中所述的错误。它还将错误位置指向
这个代码 {% url "detail" post.id %} 你可以在我最后发布的错误页面的第 44 或 45 行看到。
我尝试了很多东西,none 成功了。
我在下面发布了我所有的项目文件。你可以看看。
如果你们能帮助我,我会很高兴。
我的代码在这里:
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>
您混淆了 URL 语法。 url()
采用正则表达式;对于新式格式,您需要使用 path()
.
path("article/<int:pk>/" , views.post_detail_view.as_view() , name="detail"),
我创建了一个简单的 article/blog 应用程序。我在详细视图中遇到问题。我创建了一个文章列表视图,还创建了一个详细视图,如果有人点击其中一篇文章,它会显示文章的详细视图。
但是当我点击其中一篇文章时,出现了上述标题中所述的错误。它还将错误位置指向 这个代码 {% url "detail" post.id %} 你可以在我最后发布的错误页面的第 44 或 45 行看到。
我尝试了很多东西,none 成功了。 我在下面发布了我所有的项目文件。你可以看看。 如果你们能帮助我,我会很高兴。
我的代码在这里:
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>
您混淆了 URL 语法。 url()
采用正则表达式;对于新式格式,您需要使用 path()
.
path("article/<int:pk>/" , views.post_detail_view.as_view() , name="detail"),