找不到本地主机页面
Local host page not found
好的,我是新手,正在学习教程 https://www.youtube.com/watch?v=D6esTdOLXh4
我的问题是,当您单击指向 post:
的链接时,该视频的最后一部分显示 posting 站点的 posts
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 %}
您是否尝试过从 link 中删除“>”符号?你urls,不包含“>”符号,只有一个数字。因此,您放入 url 中的 link 不会与 urls.py 中的任何 link 连接。您还可以在 urls.py 中添加一个单独的 link,其中包含“>”,但我认为您不想这样做。
让我知道这是否有效!
你的新 link 将是:
//localhost:8000/posts/details/1/
好的,所以我找到了答案,这只是我的 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}}">
好的,我是新手,正在学习教程 https://www.youtube.com/watch?v=D6esTdOLXh4
我的问题是,当您单击指向 post:
的链接时,该视频的最后一部分显示 posting 站点的 postsPage 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 %}
您是否尝试过从 link 中删除“>”符号?你urls,不包含“>”符号,只有一个数字。因此,您放入 url 中的 link 不会与 urls.py 中的任何 link 连接。您还可以在 urls.py 中添加一个单独的 link,其中包含“>”,但我认为您不想这样做。 让我知道这是否有效!
你的新 link 将是:
//localhost:8000/posts/details/1/
好的,所以我找到了答案,这只是我的 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}}">