【问题标题】:Local host page not found找不到本地主机页面
【发布时间】:2019-11-09 17:13:21
【问题描述】:

好的,所以我是一个新学习者,正在学习一个教程https://www.youtube.com/watch?v=D6esTdOLXh4

我的问题是,当您单击帖子的链接时,该视频的最后一部分会显示发布网站的帖子:

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/posts/details/1%3E/
Using the URLconf defined in djangoproject1.urls, Django tried these URL patterns, in this order:

[name='index']
details/<int:id>/ [name='details']
admin/
posts/ [name='index']
posts/ details/<int:id>/ [name='details']
The current path, posts/details/1>/, didn't match any of these.

这是我正在编辑的 .py 和 .html 文件,它们可能是搜索错误的最佳位置

posts/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('details/<int:id>/', views.details, name='details')
]

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Posts

# Create your views here.
def index(request):
    # return HttpResponse('HELLO FROM POSTS')

    # allows post to be shown in posts connected to index.html/
    posts = Posts.objects.all()[:10]

    context = {
        'title': 'Latest Posts',
        'posts': posts
    }

    return render(request, 'posts/index.html', context)

def details(request, id):
    post = Posts.objects.get(id=id)

    context = {
        'post': post
    }

return render(request, 'posts/details.html', context)

details.html

{% extends 'posts/layout.html' %}

{% block content %}
<h3 class="center-align red lighten-3">{{post.title}}</h3>
<div class="card">
    <div class="card-content">
        {{post.body}}
    </div>
    <div class="card-action">
        {{post.created_at}}       
    </div>
</div>
<a href="/posts" class="btn">Go Back</a>
{% endblock %}

【问题讨论】:

  • 你的问题是什么?
  • 问题是为什么我在顶部收到错误。不过现在一切都很好,谢谢。
  • @StormLamas 我的回答对你有用。如果是的话,你可以检查标记它。谢谢!
  • @StormLamas 你可以勾选我的问题,如果它有效。谢谢!
  • @AgentLu 哦。不。我发布了我自己的答案。谢谢!

标签: django django-models localhost http-status-code-404


【解决方案1】:

您是否尝试过从链接中删除“>”符号。你的网址,不包含“>”符号,只有一个数字。因此,您放入 url 的链接不会与 urls.py 中的任何链接连接。您还可以在包含“>”的 urls.py 中添加一个单独的链接,但我认为您不想这样做。 让我知道这是否有效!

你的新链接是:

//本地主机:8000/posts/details/1/

【讨论】:

  • 谢谢,是的,我想解决这个问题,但我认为这只是一个无关紧要的错误或其他什么,我相信它是。无论如何,代码并没有要求“>”。
  • @StormLamas 如果对您有帮助,您可以勾选我的答案吗?谢谢!
【解决方案2】:

好的,我找到了答案,只是我的 index.py 文件中的“s”错字

{% extends 'posts/layout.html' %}

{% block content %}
<h3 class="center-align red lighten-3">{{title}}</h3>
<ul class="collection">
    <!-- allows post to be shown in posts connected to views.py/ -->
    {% for post in posts %}
        <li class="collection-item"><a href="/posts/details/{{post.id}}">{{post.title}}</a></li>
    {% endfor %}
</ul>
{% endblock %}

之前是:

<a href="/post/details/{{posts.id}}">

【讨论】:

    猜你喜欢
    • 2021-04-17
    • 2020-08-05
    • 2020-10-21
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2020-05-27
    • 2012-07-26
    • 2021-08-20
    相关资源
    最近更新 更多