我正在跟随@CoreyMSchafer的django教程。我在练习时遇到了错误,我找不到解决方法。
根据我的理解,它的问题与网址的逆转。但不能找出什么是错的
错误:/处无反向匹配
找不到参数为'(',)'的'user-posts'的反转。已尝试1个模式:['用户/(?P[^/]+)$']
由于某种原因,错误是在base.html的头,我链接引导程序。
我还尝试删除该链接,然后它给出了相同的错误,但在base.html的第0行
views.py:
class UserPostListView(ListView):
model = Post
context_object_name = 'posts'
template_name = 'blog/user_posts.html'
paginate_by = 5
def get_queryset(self):
user = get_object_or_404(User, username=self.kwargs.get('username'))
return Post.objects.all().filter(author= user).order_by('-date_posted')
urls.py 档案:
from django.urls import path, include
from .views import PostListView, PostDetailView, PostCreateView, PostUpdateView, PostDeletelView, UserPostListView
from . import views
urlpatterns = [
path('', PostListView.as_view(), name='blog-home'),
path('user/<str:username>', UserPostListView.as_view(), name='user-posts'),
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
path('post/new/', PostCreateView.as_view(), name='post-create'),
path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
path('post/<int:pk>/delete/', PostDeletelView.as_view(), name='post-delete'),
path('about/', views.about, name='blog-about'),
]
用户_帖子.html:
{% if is_paginated %}
{% if page_obj.has_previous %}
<a class="btn btn-outline-info mb-4" href="?page=1">First</a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.previous_page_number }}">Previous</a>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
<a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}">Next</a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
{% endif %}
{% endif %}
{% endblock content %}
home.html
{% if is_paginated %}
{% if page_obj.has_previous %}
<a class="btn btn-outline-info mb-4" href="?page=1">First</a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.previous_page_number }}">Previous</a>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
<a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}">Next</a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
{% endif %}
{% endif %}
post_detail.html
{% extends "blog/base.html" %}
{% block content %}
<article class="media content-section">
<img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}" alt="">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="{% url 'user-posts' object.author.username %}">{{ object.author }}</a>
<small class="text-muted">{{ object.date_posted|date:"M d, Y"}}</small>
{% if object.author == user %}
<div>
<a class="btn btn-secondary btn-sm mb-1" href="{% url 'post-update' object.id %}">Update</a>
<a class="btn btn-danger btn-sm mb-1" href="{% url 'post-delete' object.id %}">Delete</a>
</div>
{% endif %}
</div>
<h2 class="article-title">{{ object.title }}</h2>
<p class="article-content">{{ object.content }}</p>
</div>
</article>
{% endblock content %}
base.html
{% extends "blog/base.html" %}
{% block content %}
<article class="media content-section">
<img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}" alt="">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="{% url 'user-posts' object.author.username %}">{{ object.author }}</a>
<small class="text-muted">{{ object.date_posted|date:"M d, Y"}}</small>
{% if object.author == user %}
<div>
<a class="btn btn-secondary btn-sm mb-1" href="{% url 'post-update' object.id %}">Update</a>
<a class="btn btn-danger btn-sm mb-1" href="{% url 'post-delete' object.id %}">Delete</a>
</div>
{% endif %}
</div>
<h2 class="article-title">{{ object.title }}</h2>
<p class="article-content">{{ object.content }}</p>
</div>
</article>
{% endblock content %}
7条答案
按热度按时间6bc51xsx1#
只是添加到你的网址
不是这个
fruv7luv2#
我以前也有过同样的问题。在你的user_posts. html和base. html中,把所有的“object”的名称都改为“post”。例如:
这对我来说是一个解决办法。
PS:实际上,你并没有把上面提到的那部分代码发出去。XD
tjjdgumg3#
在urls.py文件中-
您忘记在
<str:username>
后添加反斜杠5lwkijsr4#
我在看同样的课程,我也遇到了同样的问题。下面两个步骤让这个对我来说很有效:首先确保你引用的是一个正确的HTML,然后在你的url后面加一个正斜杠,如下所示:
如果它不起作用,请使用url代替path,如下所示:
wydwbb8l5#
我也遇到了同样的错误。我的错误是在posts_detail.html这一行中拼错了“object”
这可能不是您出错的原因,但是其他人会坚持在HTML文件中检查拼写错误。
ibps3vxo6#
用post.author替换post.author.username。这对我很有帮助。
2uluyalo7#
我也遇到了同样的问题。
我发现替换
blog/urls.py
中的以下行:与
已解决问题。