“NoReverseMatch at /user/”,但对于另一个名为“restaurant_detail”的 url 路径,关键字参数为“{'pk'}”

如何解决“NoReverseMatch at /user/”,但对于另一个名为“restaurant_detail”的 url 路径,关键字参数为“{'pk'}”

我的页面即将完成,准备进入 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.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': '','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

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

提前致谢。

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?