应用路由:
from django.urls import path import blog.views urlpatterns = [ path('index/', blog.views.get_index_page), path('detail/<int:article_id>', blog.views.get_detail_page) ]
视图:
def get_detail_page(request, article_id): all_article = Article.objects.all() curr_article = None for article in all_article: if article.article_id == article_id: curr_article = article break section_list = curr_article.content.split('\n') return render( request, 'blog/detail.html', { 'curr_article': curr_article, 'section_list': section_list } )
前端:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Lcw</title> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </head> <body> <div class="container page-header"> <h1>{{ curr_article.title }}</h1> </div> <div class="container page-main"> <div> {% for section in section_list %} <p>{{ section }}</p> {% endfor %} </div> </div> </body> </html>
数据库:
错误:
这样访问试试 http://127.0.0.1:8000/blog/detail/4
你界面访问加多了个 / 。导致应用路由匹配不到
访问url中没有最后那个/ ?