Django 学习 Blog 实操 07 --- 博客详情支持 Markdown,代码高亮加背景色

一、本次任务

  • 博客正文支持 markdown 语法上传内容
  • 代码高亮

二、 操作步骤

安装 markdown

install markdown

解析 markdown

  • markdown 语法加入博客正文,没有解析时的显示样式
    在这里插入图片描述
  • 在详情页加入解析 markdown 语法
    detail markdown

解析 markdown
解析后的效果如下,展示的是 html 代码,不是我们想要的:
- 原因:django 出于安全方面的考虑,任何的 HTML 代码在 django 的模板中都会被转义(即显示原始的 HTML 代码,而不是经浏览器渲染后的格式)。为了解除转义,只需在模板变量后使用 safe过滤器即可,告诉 django,这段文本是安全的,你什么也不用做。

safe过滤器

post.body|safe

safe

代码高亮及修改样式

依次加入下面的三个文件的三个不同效果,知道每个的作用:

<link href="https://cdn.bootcss.com/highlight.js/9.15.8/styles/github.min.css" rel="stylesheet">
<style>
    .codehilite {
      padding: 0;
    }

    /* for block of numbers */
    .hljs-ln-numbers {
      -webkit-touch-callout: none;
      -webkit-user-select: none;
      -khtml-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;

      text-align: center;
      color: #ccc;
      border-right: 1px solid #CCC;
      vertical-align: top;
      padding-right: 5px;
    }

    .hljs-ln-n {
      width: 30px;
    }

    /* for block of code */
    .hljs-ln .hljs-ln-code {
      padding-left: 10px;
      white-space: pre;
    }
  </style>

<script src="https://cdn.bootcss.com/highlight.js/9.15.8/highlight.min.js"></script>
<script src="https://cdn.bootcss.com/highlightjs-line-numbers.js/2.7.0/highlightjs-line-numbers.min.js"></script>
<script>
hljs.initHighlightingOnLoad();
hljs.initLineNumbersOnLoad();
</script>

下图是引入 css 文件及 style 样式后的效果:
在这里插入图片描述

下图是引入第一个 js 并调用后的效果:
highlight js

下图是引入第二个 js 调用后,代码显示行号的效果:
js line number

三、提出问题

  • 怎么在文章的表单中填写正文的时候,左边是 markdown 的写法,右边可以预览效果呢?
  • 代码高亮样式的 css 可以提取到一个文件中的。(先跟着走通,后续可以自己改进)

附上 Gitee 地址:https://gitee.com/langxing/HelloDjango-blog-tutorial

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用Django自带的django-cities和django-countries应用程序的URL配置示例,并结合Vue 3前端框架进行调用的示例: 1. 首先您需要安装vue-axios和vue-router: ``` npm install vue-axios vue-router --save ``` 2. 在您的Vue 3项目中创建路由: ```javascript import { createRouter, createWebHistory } from 'vue-router' import CountryDetail from './components/CountryDetail.vue' import CityDetail from './components/CityDetail.vue' const routes = [ { path: '/countries/:code', name: 'CountryDetail', component: CountryDetail, props: true }, { path: '/cities/:id', name: 'CityDetail', component: CityDetail, props: true } ] const router = createRouter({ history: createWebHistory(), routes }) export default router ``` 3. 在您的Vue 3项目中创建组件并使用axios调用django-countries和django-cities的API: ```javascript <template> <div> <h1>{{ country.name }}</h1> <ul> <li v-for="city in cities" :key="city.id"> <router-link :to="{ name: 'CityDetail', params: { id: city.id } }">{{ city.name }}</router-link> </li> </ul> </div> </template> <script> import axios from 'axios' export default { props: { code: { type: String, required: true } }, data() { return { country: {}, cities: [] } }, async mounted() { try { const response = await axios.get(`/api/countries/${this.code}/`) this.country = response.data const citiesResponse = await axios.get(`/api/cities/?country=${this.country.id}`) this.cities = citiesResponse.data } catch (error) { console.error(error) } } } </script> ``` ```javascript <template> <div> <h1>{{ city.name }}</h1> <p>{{ city.country.name }}</p> </div> </template> <script> import axios from 'axios' export default { props: { id: { type: Number, required: true } }, data() { return { city: {} } }, async mounted() { try { const response = await axios.get(`/api/cities/${this.id}/`) this.city = response.data } catch (error) { console.error(error) } } } </script> ``` 4. 在您的Django项目的urls.py文件中配置API视图: ```python from django.urls import path from cities.views import CityDetailView, CityListAPIView from django_countries.views import country_detail, CountryListAPIView urlpatterns = [ # ... path('api/countries/', CountryListAPIView.as_view()), path('api/countries/<str:code>/', country_detail), path('api/cities/', CityListAPIView.as_view()), path('api/cities/<int:pk>/', CityDetailView.as_view()), # ... ] ``` 5. 在您的Django项目中创建API视图: ```python from django_countries import countries from cities.models import City from cities.serializers import CitySerializer from rest_framework.generics import ListAPIView, RetrieveAPIView from rest_framework.response import Response class CountryListAPIView(ListAPIView): def get(self, request): return Response(countries) class CityListAPIView(ListAPIView): serializer_class = CitySerializer def get_queryset(self): queryset = City.objects.all() country_id = self.request.query_params.get('country') if country_id: queryset = queryset.filter(country_id=country_id) return queryset ``` 以上示例是使用Django自带的django-countries和django-cities应用程序的URL配置示例,并结合Vue 3前端框架进行调用的示例,您可以根据自己的需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sapphire~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值