我正在关注@CoreyMSchafer的django教程。我在练习时出错了,找不到解决办法。
根据我的理解,它的问题是url的反转。但找不到出了什么问题
错误:/无反向匹配
找不到参数为"(",("的"用户帖子"的反向。已尝试1种模式:['user/(?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'),
]
user_posts.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 %}
url(r'^user/(?P<username>w{0,50})/$', UserPostListView.as_view(), name='user-posts'),
只需将其添加到您的url中
而不是此
path('user/<str:username>/', UserPostListView.as_view(),name='user-posts'),
我之前也有同样的问题。在user_posts.html和base.html中,将"对象"内容的所有名称更改为"发布"。示例:
"object.author" -> "post.author",
"object.title" -> "post.title",
"object.author.username" -> "post.author.username"
这对我来说是个好办法。
附言:事实上,你没有发布上面提到的代码部分。XD-
在urls.py文件中-
path('user/<str:username>/', UserPostListView.as_view(),name='user-posts'),
您忘记在<str:username>
后添加反斜杠
我看了同样的课程,遇到了同样的问题。以下两个步骤使我能够做到这一点:首先确保你引用的是正确的HTML,然后在你的url后面添加一个斜杠,如下所示:
path('user/<str:username>/', UserPostListView.as_view(), name='user-posts'),
如果它不适用于你,使用url而不是像这样的路径:
url(r'^user/(?P<username>w{0,50})/$', UserPostListView.as_view(), name='user-posts'),
我也有同样的错误。我的错误是我在posts_detail.html 这一行拼错了"object">
<a class="mr-2" href="{%url 'user-posts' object.author.username %}">{{ object.author }}</a>
这可能不是你出错的原因,但其他人坚持检查HTML文件中的拼写错误。
将post.author.username替换为post.author。它帮助了我。
我也遇到了同样的问题。
我发现替换blog/urls.py
中的以下行:
urlpatterns = [
path('user/<str:username>/', UserPostListView.as_view(), name='user-posts'),
]
带有
from django.urls import path, re_path # <- don't forget this import
urlpatterns = [
re_path(r'^user/(?P<username>w{0,50})/$', UserPostListView.as_view(), name='user-posts'),
}
解决了这个问题。