Django错误:'找不到'guestlist'并且找不到参数'('',)'。

时间:2017-07-22 13:16:54

标签: python html django

我首先要说的是,我知道这个问题出现了很多,但我无法在其他主题中找到解决方案。

尝试加载页面时出现此错误。

相关观点:

def index(request):
    all_lists = GuestList.objects.all()
    guest_list = GuestList()
    for glist in all_lists:
        guest_list = glist
    return render(request, 'guestlist/base.html', {'guest_list': guest_list})


def guestlist(request, list_id):
    g_list = get_object_or_404(GuestList, pk=list_id)
    return render(request, 'guestlist/guestlist.html', {'g_list': g_list})

guestlist / urls.py:

app_name = 'guestlist'

urlpatterns = [

    # /guestlist/
    url(r'^$', views.index, name='index'),
    # /guestlist/#/
    url(r'^(?P<list_id>[0-9]+)/$', views.guestlist, name='guestlist'),
    # /guestlist/#/isclose/
    url(r'^(?P<list_id>[0-9]+)/isclose/$', views.isclose, name='isclose'),
]

错误发生在的HTML片段:

模板:

{% load staticfiles %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %} SitDown {% endblock %}</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="{% static 'guestlist/style.css' %}"/>
    <link href="https://fonts.googleapis.com/css?family=Abel|Galdeano" rel="stylesheet"/>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

</head>
<body>
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Header -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#topNavBar">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>

            </button>
            <a class="navbar-brand" href="{% url 'guestlist:index' %}">Sit Down</a>
        </div>

        <!-- Items -->
        <div class="collapse navbar-collapse" id="topNavBar">
            <ul class="nav navbar-nav">
                <li class="active">
                    <a href="{% url 'guestlist:guestlist' guest_list.id %}">
                        <span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span>&nbsp;
                        Guest List
                    </a>
                </li>
                <li class="">
                    <a href="#">
                        <span class="glyphicon glyphicon-th" aria-hidden="true"></span>&nbsp;
                        Seating
                    </a>
                </li>
                <li class="">
                    <a href="#">
                        <span class="glyphicon glyphicon-check" aria-hidden="true"></span>&nbsp;
                        Checklist
                    </a>
                </li>
                <li class="">
                    <a href="#">
                        <span class="glyphicon glyphicon-question-sign" aria-hidden="true"></span>&nbsp;
                        Help
                    </a>
                </li>
            </ul>
            <form class="navbar-form navbar-left" role="search" method="get" action="#">
                <div class="form-group">
                    <input type="text" class="form-control" name="q" value="">
                </div>
                <button type="submit" class="btn btn default">Search</button>
            </form>
        </div>
    </div>
</nav>
{% block body %}

{% endblock %}
</body>
</html>

一切似乎都井然有序。 id被传递,它会像它应该的那样到达函数'guestlist()',但我仍然得到错误。

视图'index()'呈现'base.html',异常发生在。

编辑:

似乎当我对列表ID进行硬编码时这样:<a href="{% url 'guestlist:guestlist' 2 %}">它有效。但是当我动态地做它时却没有。我检查了变量以及两者是否提供了相同的URL。

2 个答案:

答案 0 :(得分:0)

1)根据你的问题标题,这意味着GuestList模型没有任何条目。

2)始终在url regex中使用\d+进行数字匹配,例如:(?P<list_id>\d+)

答案 1 :(得分:0)

HTML中的

,因为您传递了guestlist的列表,您必须迭代它 例如:

def index(request):
    guest_list = GuestList.objects.all()
    return render(request, 'guestlist/base.html', {'guest_list': guest_list})

和HTML页面

<body>
{% for guest in guest_list %}  <-- Start LOOP
    <a href="{% url 'guestlist:guestlist' guest.id %}"> <--- Individual Guest ID
{% endfor %}   <-- End LOOP
</body>

现在,您可以获取个人ID并将其传递到各自的guestlist视图,如果这是您想要的。