马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
报错内容如下:Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/topics/%7B%25%20url%20'learning_logs:edit_entry'%20entry.id%20%25%20%7D
Using the URLconf defined in learning_log.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
topics/ [name='topics']
topics/<int:topic_id> [name='topic']
new_topic/ [name='new_topic']
new_entry/<int:topic_id>/ [name='new_entry']
edit_entry/<int:entry_id>/ [name='edit_entry']
The current path, topics/{% url 'learning_logs:edit_entry' entry.id % }, didn’t match any of these.
urls.py内容如下:'''定义learning_logs的url模式'''
from django.urls import path
from . import views
app_name = 'learning_logs'
urlpatterns = [
#主页
path('',views.index,name = 'index'),
#显示所有的主题
path('topics/',views.topics,name='topics'),
#特定主题的详细界面
path('topics/<int:topic_id>', views.topic,name='topic'),
#用于添加新主题的页面
path('new_topic/',views.new_topic,name = 'new_topic'),
path('new_entry/<int:topic_id>/',views.new_entry,name='new_entry'),
path('edit_entry/<int:entry_id>/',views.edit_entry,name='edit_entry'),
]
view.py内容如下:from django.shortcuts import render,redirect
from .models import Topic,Entry
from .forms import TopicForm,EntryForm
# 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)
def topic(request,topic_id):
'''显示单个主题及其所有的条目'''
topic = Topic.objects.get(id=topic_id)
entries = topic.entry_set.order_by('-date_added')
context = {'topic':topic,'entries':entries}
return render(request,'learning_logs/topic.html',context)
def new_topic(request):
'''添加新主题'''
if request.method != 'POST' :
#未提交数据,创建一个新表单
form = TopicForm()
else:
#post提交数据:对数据进行处理
form = TopicForm(data=request.POST)
if form.is_valid():
form.save()
return redirect('learning_logs:topics')
#显示空表单或指出表单数据无效
context = {'form':form}
return render(request,'learning_logs/new_topic.html',context)
def new_entry(request,topic_id):
'''在特定主题中添加条目'''
topic = Topic.objects.get(id = topic_id)
if request.method != 'POST':
#未提交数据,创建一个空表单
form = EntryForm()
else:
#POST提交的数据,对数据进行处理
form = EntryForm(data = request.POST)
if form.is_valid():
new_entry = form.save(commit=False)
new_entry.topic = topic
new_entry.save()
return redirect('learning_logs:topic',topic_id = topic_id)
#显示空表单或指出表单的数据无效
context = {'topic':topic,'form':form}
return render(request,'learning_logs/new_entry1.html',context)
def edit_entry(request,entry_id):
'''编辑既有条目'''
entry = Entry.objects.get(id=entry_id)
topic = entry.topic
if request.method != 'POST':
#初次尝试请求:使用当前条目填充表单
form = EntryForm(instance=entry)
else:
#POST提交的数据:对数据进行处理
form = EntryForm(instance=entry,data=request.POST)
if form.is_valid():
form.save()
return redirect('learning_logs:topic',topic_id=topic.id)
context = {'entry':entry,'topic':topic,'form':form}
return render(request,'learning_logs/edit_entry.html',context)
网页模版 edit_entry.html:{% extends 'learning_logs/base.html' %}
{% block content %}
<p>
<a href="{% url 'learning_logs:topic',topic.id}">{{topic}}</a>
</p>
<p>Edit entry:</p>
<form action="{% url 'learning_logs:edit_entry' entry.id %}" method = 'post'>
{% csrf_token %}
{{form.as_p}}
<button name="submit">Save changes</button>
</form>
{% endblock content %}
将链接关联到topic模板 {% extends 'learning_logs/base.html' %}
{% block content %}
<p>
Topic:{{topic}}
</p>
<p>
Entries:
</p>
<p>
<a href="{% url 'learning_logs:new_entry' topic.id %}">Add new entry</a>
</p>
<ul>
{% for entry in entries %}
<li>
<p>
{{ entry.date_added | date:'M d,Y H:i'}}
</p>
<p>
{{entry.text | linebreaks}}
</p>
<p>
<a href="{% url 'learning_logs:edit_entry' entry.id % }">Edit entry</a>
</p>
</li>
{% empty %}
<li>
There are no entries for this topic yet.
</li>
{% endfor %}
</ul>
{% endblock content %}
报错的内容是:The current path, topics/{% url 'learning_logs:edit_entry' entry.id % }, didn’t match any of these.
按照这个程序,在我点击Edit entry的时候应该显示 http://localhost:8000/edit_entry/1,而报错页面的网址是:http://localhost:8000/topics/%7B%%20url%20'learning_logs:edit_entry'%20entry.id%20%%20%7D
反复查询了很多遍,也不知道 错误点在哪里
python 版本:3.9.6
Django 版本:3.2.5
请求各位大神帮助
|