嘿,我从YouTube https://www.youtube.com/watch?v=6PX_eVxg5jM频道学习django 但是我有一个问题。当我在base.html的导航中添加另一个链接时,出现此错误。
NoReverseMatch at /
Reverse for 'post-new' with no arguments not found. 1 pattern(s) tried: ['post/new/(?P<pk>[0-9]+)/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 2.2.7
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'post-new' with no arguments not found. 1 pattern(s) tried: ['post/new/(?P<pk>[0-9]+)/$']
Exception Location: C:\Users\kacpe\dev\blog\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 673
Python Executable: C:\Users\kacpe\dev\blog\Scripts\python.exe
Python Version: 3.7.5
Python Path:
['C:\\Users\\kacpe\\dev\\blog\\blog',
'C:\\Users\\kacpe\\dev\\blog\\Scripts\\python37.zip',
'C:\\Users\\kacpe\\dev\\blog\\DLLs',
'C:\\Users\\kacpe\\dev\\blog\\lib',
'C:\\Users\\kacpe\\dev\\blog\\Scripts',
'c:\\python37\\Lib',
'c:\\python37\\DLLs',
'C:\\Users\\kacpe\\dev\\blog',
'C:\\Users\\kacpe\\dev\\blog\\lib\\site-packages']
Server time: Pon, 18 Lis 2019 00:37:52 +0100
这就是我其余文件的样子。
base.html
<!DOCTYPE html>
{% load static %}
<html lang="pl">
<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.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="{% static 'myblog/main.css' %}">
{% if title %}
<title>Django Blog - {{ title }}</title>
{% else %}
<title>Django Blog</title>
{% endif %}
</head>
<body>
<header class="site-header">
<nav class="navbar navbar-expand-md navbar-dark bg-steel fixed-top">
<div class="container">
<a class="navbar-brand mr-4" href="{% url 'myblog-home' %}">Django Blog</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggle" aria-controls="navbarToggle" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarToggle">
<div class="navbar-nav mr-auto">
<a class="nav-item nav-link" href="{% url 'myblog-home' %}">Home</a>
<a class="nav-item nav-link" href="{% url 'myblog-about' %}">About</a>
</div>
<!-- Navbar Right Side -->
<div class="navbar-nav">
{% if user.is_authenticated %}
<a class="nav-item nav-link" href="{% url 'profile' %}">Profile</a>
<a class="nav-item nav-link" href="{% url 'logout' %}">Logout</a>
<a class="nav-item nav-link" href="{% url 'post-new' %}">New Post</a>
{% else %}
<a class="nav-item nav-link" href="{% url 'login' %}">Login</a>
<a class="nav-item nav-link" href="{% url 'register' %}">Register</a>
{% endif %}
</div>
</div>
</div>
</nav>
</header>
<main role="main" class="container">
<div class="row">
<div class="col-md-8">
{% if messages %}
{% for message in messages %}
<div class="alert alert-{{ message.tags }}">
{{ message }}
</div>
{% endfor %}
{% endif %}
{% block content %}{% endblock %}
</div>
<div class="col-md-4">
<div class="content-section">
<h3>Our Sidebar</h3>
<p class='text-muted'>You can put any information here you'd like.
<ul class="list-group">
<li class="list-group-item list-group-item-light">Latest Posts</li>
<li class="list-group-item list-group-item-light">Announcements</li>
<li class="list-group-item list-group-item-light">Calendars</li>
<li class="list-group-item list-group-item-light">etc</li>
</ul>
</p>
</div>
</div>
</div>
</main>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
urls.py
from django.urls import path
from . import views
from .views import PostListView, PostDetailView, PostCreateView, PostUpdateView, PostDeleteView
urlpatterns = [
path('', PostListView.as_view(), name='myblog-home'),
path('about/', views.about, name='myblog-about'),
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
path('post/new/<int:pk>/', PostCreateView.as_view(), name='post-new'),
path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
]
views.py
from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView,
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from .models import Post
# def home_page(request):
# context = {
# 'posts': Post.objects.all()
# }
# return render(request, 'myblog/home_page.html', context)
class PostListView(ListView):
model = Post
template_name = 'myblog/home_page.html'
context_object_name = 'posts'
ordering = ['-date_posted']
class PostDetailView(DetailView):
model = Post
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = ['title', 'content']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Post
fields = ['title', 'content']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
model = Post
success_url = '/'
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
def about(request):
return render(request, 'myblog/about.html', {'title': 'About'})
当我尝试{% url 'post-new' 1 %}
时,不会发生任何错误,但是当我尝试{% url 'post-new' posts.id %}
时,它会出现:
NoReverseMatch at /post/new/1/
Reverse for 'post-new' with arguments '('',)' not found. 1 pattern(s) tried: ['post/new/(?P<pk>[0-9]+)/$']
在上面的视频中,一切正常,但是我有这个问题。我一直在寻找解决方案,但没有任何帮助。感谢您的帮助。
答案 0 :(得分:0)
“创建视图”不需要pk或id,因此请按以下步骤更正您的网址:
path('post/new/', PostCreateView.as_view(), name='post-new')