【问题标题】:Django| Reverse for 'user-posts' with arguments '('',)' not found. 1 pattern(s) tried: ['user/(?P<username>[^/]+)$']姜戈|未找到带有参数 '('',)' 的 'user-posts' 的反向操作。尝试了 1 种模式:['user/(?P<username>[^/]+)$']
【发布时间】:2020-07-02 11:41:48
【问题描述】:

我正在关注@CoreyMSchafer 的 django 教程。练习时出错,找不到解决办法。

根据我的理解,它的 url 反转问题。但无法找出问题所在

错误: NoReverseMatch 在 /

未找到带有参数 '('',)' 的 'user-posts' 的反向操作。尝试了 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 'user-posts' object.author.username %}的地方 - 检查objectobject.authorobject.author.username的值是多少。这一行存在多个模板中。

标签: python django django-templates django-pagination


【解决方案1】:

我有同样的错误。我的错误是我在这行 posts_detail.html 中拼错了“object”

            <a class="mr-2" href="{%url 'user-posts' object.author.username %}">{{ object.author }}</a>

这可能不是您的错误的原因,但其他人坚持使用此错误检查您的 HTML 文件中的拼写错误。

【讨论】:

    【解决方案2】:

    我看的是同一门课程,我遇到了同样的问题。 以下两个步骤使这项工作对我有用: 首先确保您引用的是正确的 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'),
    

    【讨论】:

      【解决方案3】:

      我之前也有同样的问题。 在您的 user_posts.html 和 base.html 中, 将“对象”内容的所有名称更改为“发布”。 示例:

      "object.author" -> "post.author", 
      "object.title" -> "post.title", 
      "object.author.username" -> "post.author.username"
      

      这对我来说是一个修复。

      PS:实际上,你并没有发布上面提到的代码部分。呵呵

      【讨论】:

        【解决方案4】:
        url(r'^user/(?P<username>\w{0,50})/$', UserPostListView.as_view(), name='user-posts'),
        

        只需将其添加到您的网址中
        不是这个

        path('user/<str:username>/', UserPostListView.as_view(),name='user-posts'),
        

        【讨论】:

          【解决方案5】:

          在 urls.py 文件中 -

          path('user/<str:username>/', UserPostListView.as_view(),name='user-posts'),
          

          您忘记在&lt;str:username&gt; 之后添加反斜杠

          【讨论】:

            猜你喜欢
            • 2023-03-18
            • 2022-07-12
            • 2019-08-13
            • 1970-01-01
            • 2023-03-17
            • 2022-01-17
            • 2021-03-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多