如何在Django後台新增pagedown

2021-07-28 18:06:51 字數 4468 閱讀 3690

某公司的專案需要接入大資料,但是專案人員要求只用sql查詢。經過通宵達旦的搞了一段時間,終於小有成效。然後我們又做了個大資料開發平台**,裡面放了些開發文件。以為我們隊python比較熟悉,又不想在**上浪費過多時間,選擇django後台發布文章,前端展示。新增個pagedown

安裝依賴包(因為使用pagedown,可能不需要markdown,沒有測試過)

pip install markdown

pip install django_markdown

pip install django_pagedown

修改settings.py檔案

...#'django_markdown',

'pagedown',)

urls.py修改(可能不需要)

# url('^markdown/', include( 'django_markdown.urls')),
最重要的來了,在models.py裡

...

from pagedown.widgets import adminpagedownwidget

from django import forms

...# register your models here.

# 定義自己的form

class

document_category

( base ):

category_name = models.charfield(max_length=200)

inserttime = models.datetimefield(default=timezone.now)

class

categoryadmin

(admin.modeladmin):

list_display = ('category_name','inserttime')

search_fields = ['inserttime']

# search_fields = ['name']

admin.site.register(document_category,categoryadmin)

class

api_document

( lrcloud_base ):

title = models.charfield(max_length=200)

content = models.textfield()

category = models.foreignkey(document_category)

inserttime=models.datetimefield( default= timezone.now)

class

articleform

(forms.modelform):

content = forms.charfield(widget=adminpagedownwidget())

class

meta:

model = api_document

fields = '__all__'

class

articleadmin

(admin.modeladmin):

list_display = ('title', 'content', 'inserttime',)

search_fields = ['title']

form = articleform

#註冊的時候繫結

admin.site.register(api_document,articleadmin)

完成這些後,在提交**,在伺服器上執行

python

manage

.pyrunserver 0.0

.0.0

:8080

開啟網頁,看看是否成功了。

def __init__(self):

self.style = none

self.infile = none

print "ready to init parser tool!"

def parse(self, style=none, content=none):

#這裡是css,載入風格的,可以從外部檔案載入

css = '''a

a.absent

a.anchor

p,blockquote,ol,dl,table,pre

ul hr

body > h2:first-child

body > h1:first-child

body > h1:first-child + h2

body > h3:first-child,body > h4:first-child,body > h5:first-child,body > h6:first-child

a:first-child h1,a:first-child h2,a:first-child h3,a:first-child h4,a:first-child h5,a:first-child h6

h1 p,h2 p,h3 p,h4 p,h5 p,h6 p

li p.first

ul,ol

ul:first-child,ol:first-child

ul:last-child,ol:last-child

dl dl dt

dl dt:first-child

dl dt >:first-child

dl dt >:last-child

dl dd

dl dd >:first-child

dl dd >:last-child

blockquote

blockquote >:first-child

blockquote >:last-child

table

table tr

table tr:nth-child(2n)

table tr th

table tr td

table tr th:first-child,table tr td:first-child

table tr th:last-child,table tr td:last-child

img

span.frame

span.frame > span

span.frame span img

span.frame span span

span.align-center

span.align-center > span

span.align-center span img

span.align-right

span.align-right > span

span.align-right span img

span.float-left

span.float-left span

span.float-right

span.float-right > span

pre,code,tt

code,tt

pre code

.highlight pre

pre

pre code,pre tt '''

head = ""

"""" % (css)

try:

content = content.decode('gbk').encode('utf-8')

except:

content = content

content = markdown(content, extensions=['markdown.extensions.extra', 'markdown.extensions.codehilite'])

return head+ content

從外部呼叫函式後,返回的是html的乙個字串,然後再模板裡,吧這些東東放進去。

def

show_document

(request):

documentid = request.get['id']

doc = api_document.objects.get(id=documentid)

p = md2html.parser()

ret = p.parse('', doc.content)

c = context()

t = loader.get_template('apis.html')

html = t.render(c)

response = httpresponse(html)

在模板檔案apis.html裡,要這樣使用

}

一定要加safe,否則…你懂得吧,試試就知道了~

如何在django中新增日誌功能

官方文件 猛戳這裡 在settings中配置以下 logging dir 日誌檔案存放目錄 logging dir logs 日誌存放路徑 if not os.path.exists logging dir os.mkdir logging dir import logging logging fi...

ecshop如何在後台管理左側選單新增新欄目並管理

在對ecshop進行二次開發的時候,在後台增加一些功能,下面給大家說說,ecshop如何在後台新增新欄目 首先要找到相關的檔案,這些檔案時我們要修改的檔案 1 admin 檔案下admin includes inc menu.php 2 languages zh cn admin common.ph...

如何在django系統外使用django的ORM

有時候我們希望在乙個新的獨立指令碼裡面使用django的orm系統。例如我曾在乙個django專案裡面,用tornado單獨實現了其中的im 即時通訊 功能。如果直接import django的models的話,會出現以下錯誤 django.core.exceptions.improperlycon...