如圖:

urls.py
"""定義learning_logs的URL模式"""
from django.urls import path,re_path
from . import views
urlpatterns = [
#主頁
path('', views.index, name='index'),
#顯示所有主題
path('topics/', views.topics, name='topics'),
#顯示所有主題
re_path('topics/(?P<topic_id>\d+)/', views.topics, name='topic'),
#特定主題的詳細頁面
re_path('topics/(?P<topic_id>\d+)/', views.topic, name='topic'),
#用于添加新主題的網頁
re_path('new_topic/', views.new_topic, name='new_topic'),
#用于添加新條目的頁面
re_path('new_entry/(?P<topic_id>\d+)/', views.new_entry, name='new_entry'),
]
app_name = 'learning_logs'
views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from .models import Topic
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(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('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=https://bbs.csdn.net/topics/request.POST)
if form.is_valid():
new_entry = form.save(commit=False)
new_entry.topic = topic
new_entry.save()
return HttpResponseRedirect(reverse('learning_logs:topic', args=[topic_id]))
context = {'topic': topic, 'form': form}
return render(request, 'learning_logs/new_entry.html', context)
topics.html
{% extends "learning_logs/base.html" %}
{% block content %}
<p>Topics</p>
<ul>
{% for topic in topics %}
<li>
<a href="https://bbs.csdn.net/topics/{% url 'learning_logs:topic' topic.id %}">{{ topic }}</a>
</li>
{% empty %}
<li>No topics have been added yet.</li>
{% endfor %}
</ul>
<a href="https://bbs.csdn.net/topics/{% url 'learning_logs:new_topic' %}">Add a new topic:</a>
{% endblock content %}
topic.html
{% extends "learning_logs/base.html" %}
{% block content %}
<p>Topic:{{ topic }}</p>
<p>Entries:</p>
<p>
<a href="https://bbs.csdn.net/topics/{% 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>
</li>
{% empty %}
<li>
There are no entries for this topic yet.
</li>
{% endfor %}
</ul>
{% endblock content %}
其他頁面還可以進,就是特定主題的界面進不去

求助各位大佬啊,不想自己的python生涯被一個小BUG斷掉,哭嚶嚶
python版本是3.6,Django是2.0
uj5u.com熱心網友回復:
如果我沒看錯#顯示所有主題
re_path('topics/(?P<topic_id>\d+)/', views.topics, name='topic'),
#特定主題的詳細頁面
re_path('topics/(?P<topic_id>\d+)/', views.topic, name='topic'),
你兩個url的正則一模一樣,可能是這里出問題了吧。
我猜測,特定主題的詳細頁面正則該是topic/(?P......吧?
uj5u.com熱心網友回復:
#顯示所有主題re_path('topics/(?P<topic_id>\d+)/', views.topics, name='topic'),這句注釋掉試試。
uj5u.com熱心網友回復:
你找到原因了嗎?我和你相同的問題轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/31629.html
