我正在学习 Python 速成类(class)中的第 18 章 18.4.2,当我打开 http://localhost:8000/topics 时,我正在使用 Django 3.0 和 python 3.8 显示
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/topics
Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
The current path, topics, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
这是我的代码 学习日志\urls.py
from django.contrib import admin
from django.urls import path
from learning_logs import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.index,name = 'index')
]
学习日志\urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.index,name = 'index'),
url(r'^topics/$',views.topics,name='topics')
]
View .py
from django.shortcuts import render
from .models import Topic
# Create your views here.
def index(request):
return render(request,'learning_logs/index.html')
def topics(request):
topics = Topic.objects.order_by('date_added')
context = {'topics':topics}
return render(request,'learning_logs/topics.html',context)
base.html
<p>
<a href="{% url 'learning_logs:index' %}">Learning Log</a>-
<a href="{% url 'learning_logs:topics'%}">Topics</a>
</p>
{% block content %}{% endblock content %}
主题.html
{% extends "learning_logs/base.html" %}
{% block content %}
<p>Topics</p>
<ul>
{% for topic in topics %}
<li>{{ topic }}</li>
{% empty %}
<li>No topics have been added yet.</li>
{% endfor %}
</ul>
{% endblock content %}
并且运行服务器显示:
Not Found: /topics
[06/Jan/2020 17:53:15] "GET /topics HTTP/1.1" 404 2077
最佳答案
首先,这是个好问题。喜欢细节。
错误如下
Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order
这意味着无法找到网址/topics
。您需要将该 url 从应用程序特定的 urls.py 导入到主 urls.py 中。这通常是通过类似
# in main urls.py
from learning_logs.urls import urlpatterns as ll_urlpatterns
# other patterns ...
urlpatterns += ll_urlpatterns
关于python - Django 显示页面未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59609983/