找不到本地主机页面

时间:2019-06-27 16:18:16

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

好的,我是一名新手,正在学习教程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 %}

2 个答案:

答案 0 :(得分:0)

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

您的新链接为:

/// localhost:8000 / posts / details / 1 /

答案 1 :(得分:0)

好的,所以我找到了答案,这只是我的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}}">