本文是创建一个简单的pastebin代码高亮Web API
Copy virtualenv env
source env / bin / activate
pip install django
pip install djangorestframework
pip install pygments # We'll be using this for the code highlighting
Copy django-admin.py startproject tutorial
cd tutorial
python manage.py startapp snippets
这里采用的目录结构是project
和app
并列。有关Django目录结构和风格,参考Django项目代码架构风格
Copy INSTALLED_APPS = (
...
'rest_framework' ,
'snippets.apps.SnippetsConfig' ,
)
注意:如果Django < 1.9,则需要将上面的snippets.apps.SnippetsConfig
替换成snippets
。
创建一个用于工作的模型(a model to work with)
Copy # -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django . db import models
# Create your models here.
from pygments . lexers import get_all_lexers
from pygments . styles import get_all_styles
LEXERS = [item for item in get_all_lexers () if item [ 1 ] ]
LANGUAGE_CHOICES = sorted ([(item[ 1 ][ 0 ], item[ 0 ]) for item in LEXERS])
STYLE_CHOICES = sorted ((item, item) for item in get_all_styles ())
class Snippet ( models . Model ):
created = models . DateTimeField (auto_now_add = True )
title = models . CharField (max_length = 100 , blank = True , default = '' )
code = models . TextField ()
linenos = models . BooleanField (default = False )
language = models . CharField (choices = LANGUAGE_CHOICES, default = 'python' , max_length = 100 )
style = models . CharField (choices = STYLE_CHOICES, default = 'friendly' , max_length = 100 )
class Meta :
ordering = ( 'created' , )
Copy python manage.py makemigrations snippets
python manage.py migrate