为什么会这样?我什么都按照指示做。我学习。你知道吗
你知道吗网址.py你知道吗
from django.conf.urls import url,include
from django.contrib import admin
from . import views
urlpatterns = [
#url(r'^$',views.main,name="main"),
url(r'^post/(?P<id>[0-9]+)/', views.post_detail, name='post_detail'),
]
你知道吗视图.py你知道吗
from django.shortcuts import render
from django.utils import timezone
from .models import Post
from django.shortcuts import render, get_object_or_404
# Create your views here.
def post_detail(request,id):
post = get_object_or_404(Post, pk=id)
return render(request, 'main/post_detail.html', {'post': post})
岗位_详细信息.html你知道吗
{% extends 'main/base.html' %}
{% block content %}
<div class="post">
{% if post.published_date %}
<div class="date">
{{ post.published_date }}
</div>
{% endif %}
<h1>{{ post.title }}</h1>
<p>{{ post.text|linebreaksbr }}</p>
</div>
{% endblock %}
怎么办?即使不是id放pk,但没什么帮助。你知道吗
你的网址.py文件应该工作(它为我工作),但你应该以$结束此url,以防止与此视图的产品id之后的内容匹配的url。你知道吗
您可以使用pdb in
post_detail
方法检查是否调用了:如果方法被调用,那么执行应该在
pdb.set_trace()
行停止。您可以按“c”和“回车”继续。 pdb是非常有用的,当你有问题的时候应该首先使用它。(模式详细信息:https://docs.python.org/2/library/pdb.html)如果方法被调用,则应该是缺少具有给定id的post的问题,否则应该确保设置中的
ROOT_URLCONF
引用了此模块。你知道吗要检查是否存在此id的post:
在django shell中尝试以下命令:
如果它回来了
而不是使用以下方法创建此文章:
相关问题 更多 >
编程相关推荐