如何通过Django模板引擎展示页面内容?

2026-05-21 14:231阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计985个文字,预计阅读时间需要4分钟。

如何通过Django模板引擎展示页面内容?

使用Django模板展示内容,当数据模型建立完成后,需要将模型中的数据展示出来。这是通过模板这种类型进行显示的,查看文章页面,可以通过一个处理方法获取文章的唯一标识,然后查询数据库获取文章信息。

Django使用模板显示内容

当数据模型建立完成时,接下来需要将模型里的数据展示出来,而且是通过模板这种类型进行显示

如何通过Django模板引擎展示页面内容?

查看文章页面

  • 如何通过一个处理方法获取文章唯一标识
    通过查看article/migrations/0001_initial.py可得出,表格中有"id","title","content"
  • 通过编辑article/views.py编写处理方法
  • from django.127.0.0.1:8000/article/1 页面返回: 文章id: 1

  • 通过访问127.0.0.1:8000/article/2 页面返回: 文章id: 2
  • 应用模型

  • 模型的objects是获取或操作模型的对象
  • Article.objects.get(条件)
    Article.objects.all()
    Article.objects.filter(条件)

  • 如何在articleAPP中的views.py引用articleAPP下的models.py中的模型数据,参考如下:
  • article/views.py
    from .models import Article

    def article_detail(request, article_id):
    article = Article.objects.get(id=article_id)
    return HttpResponse("文章的标题", article.title) # 获取模型数据中的数据

  • 程序测试
    • 通过访问127.0.0.1:8000/article/1 页面返回: 文章的标题: one
    • 通过访问127.0.0.1:8000/article/2 页面返回: 文章的标题: two
    • 通过访问127.0.0.1:8000/article/4 页面返回: DoesNotExist 异常

  • 异常情况
  • 异常处理
    通过编写article/views.py
  • from .models import Article

    def article_detail(request, article_id):
    try:
    article = Article.objects.get(id=article_id)
    except Article.DoesNotExist:
    return HttpResponse("不存在") # 返回不存在
    return HttpResponse("文章的标题", article.title) # 获取模型数据中的数据

    或者

    通过编写article/views.py

    from .models import Article
    from django.127.0.0.1:8000/article/1 页面返回:

  • 优化404方法
  • from django.shortcuts import render, get_object_or_404
    from .models import Article

    # Create your views here.
    def article_detail(request,article_id): # 创建文章明细处理方法
    article = get_object_or_404(Article, pk=article_id)
    context = {} # 定义字典
    context['article_obj'] = article
    return render(request, "article_detail.html", context)

  • 测试结果
    • 通过访问127.0.0.1:8000/article/4 页面返回:

    获取文章列表

  • 为什么要获取文章列表
  • 通过编写articleAPP中的views.py文件
  • from django.shortcuts import render_to_response # 引用 render_to_response 方法
    def article_list(request):
    article = Article.objects.all()
    context = {}
    context['articles'] = articles
    return render_to_response("article_list.html", context)

  • 在templates文件夹中创建article_list.html
  • <html>
    <body>
    {{ articles }}
    </body>
    </html>

  • 编写urls.py总路由文件
  • from article.views import article_list # 引用article_list
    path('article/', article_list, name="article_list") # 增加

  • 测试
    • 通过访问​​127.0.0.1:8000/article/​​ 页面返回:
  • 通过for循环 继续编写article_list.html
  • <html>
    <body>
    {% for article in articles %}
    <a href="/article/{{ article.pk }}">{{ article.title }}</a> <!--引用views.py中的pk,参考 "article = get_object_or_404(Article, pk=article_id)"-->
    {% endfor%}
    </body>
    </html>

  • 测试
    • 通过访问​​127.0.0.1:8000/article/​​ 页面返回:
    • 点击 one
    • 获取文章列表处理成功

    总urls包含app的urls


  • 逻辑图如下
  • ​​在articleAPP中创建urls.py​​,并编辑如下
  • from django.urls import path
    from . import views

    urlpatterns = [
    # localhost:8000/article/
    path('' , views.article_list , name="article_list") ,
    # localhost:8000/article/1
    path("<int:article_id>" , views.article_detail , name="article_detail") ,
    ]

  • 编写总路由mysite/urls.py
  • from django.contrib import admin
    from django.urls import path
    from . import views
    from django.urls import include # 使用include库
    urlpatterns = [
    path('admin/', admin.site.urls),
    path("", views.index),
    path("article/", include("article.urls")) # 引用APP中的urls路由
    ]

  • 测试
    • 测试正常

    本文共计985个文字,预计阅读时间需要4分钟。

    如何通过Django模板引擎展示页面内容?

    使用Django模板展示内容,当数据模型建立完成后,需要将模型中的数据展示出来。这是通过模板这种类型进行显示的,查看文章页面,可以通过一个处理方法获取文章的唯一标识,然后查询数据库获取文章信息。

    Django使用模板显示内容

    当数据模型建立完成时,接下来需要将模型里的数据展示出来,而且是通过模板这种类型进行显示

    如何通过Django模板引擎展示页面内容?

    查看文章页面

  • 如何通过一个处理方法获取文章唯一标识
    通过查看article/migrations/0001_initial.py可得出,表格中有"id","title","content"
  • 通过编辑article/views.py编写处理方法
  • from django.127.0.0.1:8000/article/1 页面返回: 文章id: 1

  • 通过访问127.0.0.1:8000/article/2 页面返回: 文章id: 2
  • 应用模型

  • 模型的objects是获取或操作模型的对象
  • Article.objects.get(条件)
    Article.objects.all()
    Article.objects.filter(条件)

  • 如何在articleAPP中的views.py引用articleAPP下的models.py中的模型数据,参考如下:
  • article/views.py
    from .models import Article

    def article_detail(request, article_id):
    article = Article.objects.get(id=article_id)
    return HttpResponse("文章的标题", article.title) # 获取模型数据中的数据

  • 程序测试
    • 通过访问127.0.0.1:8000/article/1 页面返回: 文章的标题: one
    • 通过访问127.0.0.1:8000/article/2 页面返回: 文章的标题: two
    • 通过访问127.0.0.1:8000/article/4 页面返回: DoesNotExist 异常

  • 异常情况
  • 异常处理
    通过编写article/views.py
  • from .models import Article

    def article_detail(request, article_id):
    try:
    article = Article.objects.get(id=article_id)
    except Article.DoesNotExist:
    return HttpResponse("不存在") # 返回不存在
    return HttpResponse("文章的标题", article.title) # 获取模型数据中的数据

    或者

    通过编写article/views.py

    from .models import Article
    from django.127.0.0.1:8000/article/1 页面返回:

  • 优化404方法
  • from django.shortcuts import render, get_object_or_404
    from .models import Article

    # Create your views here.
    def article_detail(request,article_id): # 创建文章明细处理方法
    article = get_object_or_404(Article, pk=article_id)
    context = {} # 定义字典
    context['article_obj'] = article
    return render(request, "article_detail.html", context)

  • 测试结果
    • 通过访问127.0.0.1:8000/article/4 页面返回:

    获取文章列表

  • 为什么要获取文章列表
  • 通过编写articleAPP中的views.py文件
  • from django.shortcuts import render_to_response # 引用 render_to_response 方法
    def article_list(request):
    article = Article.objects.all()
    context = {}
    context['articles'] = articles
    return render_to_response("article_list.html", context)

  • 在templates文件夹中创建article_list.html
  • <html>
    <body>
    {{ articles }}
    </body>
    </html>

  • 编写urls.py总路由文件
  • from article.views import article_list # 引用article_list
    path('article/', article_list, name="article_list") # 增加

  • 测试
    • 通过访问​​127.0.0.1:8000/article/​​ 页面返回:
  • 通过for循环 继续编写article_list.html
  • <html>
    <body>
    {% for article in articles %}
    <a href="/article/{{ article.pk }}">{{ article.title }}</a> <!--引用views.py中的pk,参考 "article = get_object_or_404(Article, pk=article_id)"-->
    {% endfor%}
    </body>
    </html>

  • 测试
    • 通过访问​​127.0.0.1:8000/article/​​ 页面返回:
    • 点击 one
    • 获取文章列表处理成功

    总urls包含app的urls


  • 逻辑图如下
  • ​​在articleAPP中创建urls.py​​,并编辑如下
  • from django.urls import path
    from . import views

    urlpatterns = [
    # localhost:8000/article/
    path('' , views.article_list , name="article_list") ,
    # localhost:8000/article/1
    path("<int:article_id>" , views.article_detail , name="article_detail") ,
    ]

  • 编写总路由mysite/urls.py
  • from django.contrib import admin
    from django.urls import path
    from . import views
    from django.urls import include # 使用include库
    urlpatterns = [
    path('admin/', admin.site.urls),
    path("", views.index),
    path("article/", include("article.urls")) # 引用APP中的urls路由
    ]

  • 测试
    • 测试正常