【问题标题】:"NoReverseMatch at /user/" but for an other url path called '"restaurant_detail' with keyword arguments "{'pk'}"“NoReverseMatch at /user/”,但对于另一个名为“restaurant_detail”的 URL 路径,关键字参数“{'pk'}”
【发布时间】:2021-02-05 12:04:47
【问题描述】:

我的页面即将完成,准备好进入 alpha 阶段,但随后我收到此错误“NoReverseMatch at /user/”。这通常意味着从 urls.py 指向用户链接到的页面的路径有问题。但在这种情况下,我打开页面 /user/ 并得到 /restaurant_detail/int:pk 的错误。我告诉你所有的细节。不要怀疑。 HTML 元素是德文的。 :)

被点击的链接:

<a href="{% url 'user' %}">Profil</a>

urls.py中的路径:

path('user/', views.user, name='user'),

错误所指的路径:

path('restaurant_detail/<str:pk>/', views.restaurant_detail, name='restaurant_detail'),
path('restaurant_detail/<str:pk>/<int:underpage>', views.restaurant_detail, name='restaurant_detail'),

我认为这不是这个问题的一部分,但这是views.py中用户页面的视图:

@login_required(login_url='login')
def user(request):
    currentUser = request.user
    comments = Comment.objects.filter(account=request.user)
    liked = Restaurant.objects.filter(likes=currentUser)
    foods = Food.objects.all()

    if request.method == 'POST':
        #comment_form = CreateCommentForm(request.POST or None)
        userForm = UserUpdateForm(request.POST, instance=currentUser)
        pictureForm = PictureUpdateForm(request.POST,
                                        request.FILES,
                                        instance=currentUser)
        if userForm.is_valid():

            userForm.save()
            pictureForm.save()
            messages.success(request, f'Your account hast been updated')
            return redirect('user')

    else:
        userForm = UserUpdateForm(instance=currentUser)
        pictureForm = PictureUpdateForm(instance=currentUser)
        #comment_form = CreateCommentForm()

    context = {'user': currentUser, 'userForm': userForm, 'pictureForm': pictureForm, 'comments': comments, 'liked': liked, 'foods': foods}
    return render(request, 'accounts/user.html', context)

对 url 的 HTML 引用:

<div id="user_saved_display" class="user_options">
    {% for i in liked %}
    <div class='restaurants_block' data-name="restaurant{{ forloop.counter0 }}">
        <div class="restaurants_img">
        {% ifequal i.restaurant_picture None %}
            <img src="{% static 'assets/dashboard-BG.jpg' %}">
        {% else %}
            <img src="{{i.restaurant_picture.url}}">
        {% endifequal %}

        </div>
        <div class="restaurants_name">
            <p><a class="restaurants_name_link" href="{% url 'restaurant_detail' pk=i.pk underpage=0 %}">{{i.name}}</a></p>
        </div>

和其他html:

<div id="user_comments">
        {% for comment in comments reversed %}
            <div class="rd_comment">
            <div class="display_comment">
            <div class="rd_comment_profilepic">
            {% if comment.restaurant.restaurant_picture %}
            <img class="rd_profilepic" src="{{comment.restaurant.restaurant_picture.url}}" alt="Profilbild">
            {% else %}
            <img class="rd_profilepic" src="{% static 'assets/PO_Icon_White_BG.png' %}" alt="Profilbild">
            {% endif %}
            </div>
            <a href="{% url 'restaurant_detail' pk=comment.restaurant.pk underpage=0 %}"><div class="rd_comment_username">{{ comment.restaurant.name }}</div></a>
            <div class="rd_comment_time">{{ comment.date_created }}</div>
            <div class="rd_comment_rating">
            <p> {{ comment.ratings }} </p>
            </div>
            <div class="rd_comment_text">{{ comment.content }}</div>
            </div>
            </div>
        {% endfor %}
    </div>

我从 Django 得到的回溯

NoReverseMatch at /user/
Reverse for 'restaurant_detail' with keyword arguments '{'pk': '', 'underpage': 0}' not found. 2 pattern(s) tried: ['restaurant_detail/(?P<pk>[^/]+)/(?P<underpage>[0-9]+)$', 'restaurant_detail/(?P<pk>[^/]+)/$']
Request Method: GET
Request URL:    http://127.0.0.1:8000/user/
Django Version: 3.1.5
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'restaurant_detail' with keyword arguments '{'pk': '', 'underpage': 0}' not found. 2 pattern(s) tried: ['restaurant_detail/(?P<pk>[^/]+)/(?P<underpage>[0-9]+)$', 'restaurant_detail/(?P<pk>[^/]+)/$']
Exception Location: /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/urls/resolvers.py, line 685, in _reverse_with_prefix
Python Executable:  /Library/Frameworks/Python.framework/Versions/3.9/bin/python3
Python Version: 3.9.0
Python Path:    
['/Users/lucasfalkowsky/django/PlantyOptions',
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python39.zip',
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9',
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages',
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pgxnclient-1.3.1-py3.9.egg',
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/six-1.15.0-py3.9.egg',
 '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/esptool-3.1.dev0-py3.9.egg']
Server time:    Fri, 05 Feb 2021 12:33:03 +0000

我希望我的问题不是什么新鲜事,有人知道发生了什么。也许我只是看这段代码太久了,并监督了一些重要的事情。

提前致谢。

【问题讨论】:

  • 尝试将path('restaurant_detail/&lt;str:pk&gt;/'... 更改为path('restaurant_detail/&lt;int:pk&gt;/'...path('restaurant_detail/&lt;pk&gt;/'...
  • 该错误表示在为用户渲染模板时,有一个指向restaurant_detail的链接无法反转,请添加相关模板。
  • 嗨 Jacek,感谢您的快速回答。我尝试过并立即重复它,但是搜索除字符串之外的 Int 时出错。你有别的想法吗?
  • 您好,您能粘贴完整的回溯吗?
  • pk=comment.restaurant.pk 评论一定有餐厅还是可能为空?

标签: python django django-urls


【解决方案1】:

问题是一些 cmets 卡在数据库中,在删除餐厅后没有分配给他们的餐厅。我删除了剩余的 cmets 并将 on_delete=CASCADE 添加到评论类中。

【讨论】:

    猜你喜欢
    • 2021-08-24
    • 2018-05-13
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 2018-09-08
    • 2017-06-07
    • 2012-12-11
    • 1970-01-01
    相关资源
    最近更新 更多