Python 无法解决';阅读《华盛顿邮报》';带参数';(';';,)';没有找到。尝试了1种模式:[';read#u post/(?P<;id>;[0-9]+;)$';]
我的模板中有一个导航栏,我试图在其中添加一个活动类,但是,每当我这样做时,都会出现以下错误:Python 无法解决';阅读《华盛顿邮报》';带参数';(';';,)';没有找到。尝试了1种模式:[';read#u post/(?P<;id>;[0-9]+;)$';],python,html,django,Python,Html,Django,我的模板中有一个导航栏,我试图在其中添加一个活动类,但是,每当我这样做时,都会出现以下错误: NoReverseMatch at / Reverse for 'read_post' with arguments '('',)' not found. 1 pattern(s) tried: ['read_post/(?P<id>[0-9]+)$'] Request Method: GET Request URL: http://127.0.0.1:8000/ Django Ver
NoReverseMatch at /
Reverse for 'read_post' with arguments '('',)' not found. 1 pattern(s) tried: ['read_post/(?P<id>[0-9]+)$']
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.1.7
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'read_post' with arguments '('',)' not found. 1 pattern(s) tried: ['read_post/(?P<id>[0-9]+)$']
Exception Location: C:\Users\Abdullah\AppData\Local\Programs\Python\Python39\lib\site-packages\django\urls\resolvers.py, line 685, in _reverse_with_prefix
Python Executable: C:\Users\Abdullah\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.2
Python Path:
['C:\\Users\\Abdullah\\Desktop\\Blog\\my_blog',
'C:\\Users\\Abdullah\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
'C:\\Users\\Abdullah\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
'C:\\Users\\Abdullah\\AppData\\Local\\Programs\\Python\\Python39\\lib',
'C:\\Users\\Abdullah\\AppData\\Local\\Programs\\Python\\Python39',
'C:\\Users\\Abdullah\\AppData\\Roaming\\Python\\Python39\\site-packages',
'C:\\Users\\Abdullah\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']
Server time: Wed, 21 Apr 2021 09:38:37 +0000
这是我的博客app/posts.html:
`{% extends 'blog_app/base.html' %}
{% block content %}
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li {% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% for post in posts %}
<div>
<h2>{{ post.title }}</h2>
<a href="{% url 'read_post' post.id %}"><button>Read blog</button></a>
<h4>Posted by {{ post.author }} on {{ post.datetime }}</h4>
<br>
</div>
{% endfor %}
{% endblock %}`
另外,请建议添加活动链接类的方法,因为我无法做到这一点。显示
blog\u app/posts.html
的内容。除查看Post
外,还要删除所有其他视图代码。刚才,请看一看,您是否自己在Post
模型上设置了一些主键?尝试编写{%url'read_post'post.pk%}
而不是{%url'read_post'post.id%}
(post.pk
而不是post.id
)。如果这不起作用,请显示您的Post
模型。我没有设置主键,因为我认为没有必要设置主键。另外,我不认为有必要将id传递到{%url'posts'post.pk%}。但是我没有说任何关于{%url'posts'post.pk%}
的内容?我说的是blog\u app/posts.html
中的{%url'read\u post'post.id%}
行。
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from blog_app import views
from django.contrib.auth import views as auth_views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.posts, name='posts'),
path('signup', views.signup, name="signup"),
path('login', auth_views.LoginView.as_view(template_name='blog_app/login.html'), name='login'),
path('logout', auth_views.LogoutView.as_view(template_name='blog_app/logout.html'), name='logout'),
path('post', views.post, name='post'),
path('myposts', views.myposts, name='myposts'),
path('delete_post/<int:id>', views.delete_post, name='delete_post'),
path('edit_post/<int:id>', views.edit_post, name='edit_post'),
path('read_post/<int:id>', views.read_post, name='read_post')
]`
`def posts(request):
posts = Post.objects.all()
return render(request, 'blog_app/posts.html', {'posts': posts})
`
`{% extends 'blog_app/base.html' %}
{% block content %}
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li {% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% for post in posts %}
<div>
<h2>{{ post.title }}</h2>
<a href="{% url 'read_post' post.id %}"><button>Read blog</button></a>
<h4>Posted by {{ post.author }} on {{ post.datetime }}</h4>
<br>
</div>
{% endfor %}
{% endblock %}`
`from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from datetime import datetime
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
author = models.ForeignKey(to=User, on_delete=models.CASCADE)
date_time = timezone.now()
# datetime = models.DateTimeField(auto=timezone.now)`