python Django:找不到页面

2024-04-16 23:44:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我到处找遍了,都找不到解决问题的办法。这是django抛出的错误。这个错误被抛出时,在我的主页上,我有一个五个链接,点击后应该会引导你到一个详细的视图所说的链接。在

Using the URLconf defined in untitled.urls, Django tried these URL patterns, in this order:

    ^$
    ^index/ ^$ [name='index']
    ^index/ ^(?P<distro_id>[0-9]+)/$ [name='distro_id'] 
    ^admin/

The current URL, index//, didn't match any of these.

据我所知,我不明白为什么会抛出这个错误。在

这是我的网址.py在

^{pr2}$

我的索引/网址.py在

^{3}$

我的视图.py在

from django.shortcuts import get_object_or_404, render
from django.template import loader, RequestContext
from django.http import Http404
from .models import Distros


def index(request):
    all_distros = Distros.objects.all()
    context = {'all_distros': all_distros, }
    return render(request, 'index/index.html', context)


def detail(request, distro_id,):
    distro_id = get_object_or_404 (Distros, pk=distro_id)
    return render(request, 'index/detail.html', {'distro_id': distro_id})

模板代码:

    {% extends 'index/base.html' %}

{% block body %}
<ul>
    {% for distro in all_distros %}
        <li><a href="/index/{{ distro_id }}/">{{ index.distro_id }}</a></li>
    {% endfor %}
</ul>
{% endblock %}

我相信这些文件都是相关的。我相信所有的设置都是正确的,所以我不知道为什么会抛出错误。我肯定我错过了一些简单的事情,我只是忽略了。在


Tags: djangoinfrompyimportidindexrequest
1条回答
网友
1楼 · 发布于 2024-04-16 23:44:40

请不要使用硬编码的网址,因为它们很容易出错,就像你的情况。而不是:

<a href="/index/{{ index.distro.id }}/">

url模板标记与命名空间(index)和视图名称(distro_id)一起使用:

^{pr2}$

注意,index.distro.id也有一个错误,因为index实际上是一个Distros对象。它有一个id字段,但没有distro.id。在

相关问题 更多 >