现在我主要教大家如何去实战,做一个简易的知乎日报API
首先你要熟悉django的基本用法,会写模型,会写视图函数,会配置url。
1.配置字符编码
因为我们等一下要使用中文,所以要先设好字符编码
在settings.py里将LANGUAGE_CODE设为'zh-CN'
然后添加这两行
FILE_CHARSET='utf-8'
DEFAULT_CHARSET='utf-8'
还要进入到数据库
依次输入
1
2
3
4
5
6
|
set character_set_client = utf8 ; set character_set_connection = utf8 ; set character_set_database = utf8 ; set character_set_results = utf8 ; set character_set_server = utf8 ; set character_set_system = utf8 ; |
设置完成之后,输入
show variables like 'character%';
对照一下结果是否是这样
2.然后开始写模型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class News(models.Model): created = models.DateTimeField(auto_now_add = True ) title = models.CharField(max_length = 100 ) image = models.CharField(max_length = 100 ) theme_id = models.IntegerField() class Meta: ordering = ( 'created' ,) def __unicode__( self ): return self .title class NewsDetail(models.Model): news = models.ForeignKey(News) created = models.DateTimeField(auto_now_add = True ) content = models.CharField(max_length = 1000 ) image = models.CharField(max_length = 99 ) class Meta: ordering = ( 'created' ,) def __unicode__( self ): return self .news.title |
这里不难理解,这里我分两层,第一层是概括,第二层是详细内容,然后以概括的作为外键
概括主要包含标题,配图地址和主题
详细内容包括内容和配图地址
3.将模式序列化
这是rest_framework非常强大的一点
在app里新建一个serializers.py,然后创建继承自ModelSerializer的类
1
2
3
4
5
6
7
8
9
10
|
from rest_framework import serializers class NewsSerializer(serializers.ModelSerializer): class Meta: model = News fields = ( 'id' , 'title' , 'image' , 'theme_id' ) class NewsDetailSerializer(serializers.ModelSerializer): class Meta: model = NewsDetail fields = ( 'id' , 'image' , 'content' ) |
model为模型,fields为你想要查询显示的字段
4.然后再写视图函数
rest_framework.renderers中的JSONRenderer可以将对象渲染为json形式的字符串
1
2
3
4
5
6
7
8
9
10
11
|
from rest_framework.renderers import JSONRenderer class JSONResponse(HttpResponse): """ 用于返回JSON数据. """ def __init__( self , data, * * kwargs): content = JSONRenderer().render(data) kwargs[ 'content_type' ] = 'application/json' content = '{"news":' + content + '}' super (JSONResponse, self ).__init__(content, * * kwargs) |
我们在字符串外面在包一个news,这样获取比较方便
如何像知乎日报的那样获取最新的几个news呢
1
2
3
4
5
6
7
8
9
|
@csrf_exempt def latest_news(request): """ 展示最新的10个news. """ if request.method = = 'GET' : news = News.objects. all ()[: 10 ] serializer = NewsSerializer(news, many = True ) return JSONResponse(serializer.data) |
因为返回的是一个集合所以NewsSerializer的many参数要设为True
返回某个主题的前几个news
1
2
3
4
5
6
7
8
9
|
@csrf_exempt def theme_news(request,theme_id): """ 展示某个主题的前10个news. """ if request.method = = 'GET' : news = News.objects. filter (theme_id = theme_id)[: 10 ] serializer = NewsSerializer(news, many = True ) return JSONResponse(serializer.data) |
返回某个id的news的详细内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@csrf_exempt def news_detail(request,news_id): """ 显示某个news的内容. """ try : news = NewsDetail.objects.get(news_id = news_id) except Snippet.DoesNotExist: return HttpResponse(status = 404 ) if request.method = = 'GET' : serializer = NewsDetailSerializer(news) # print serializer.data return JSONResponse(serializer.data) |
这里获取的是单个对象所以不用加many参数
5.配置url
1
2
3
4
5
6
|
urlpatterns = [ ... url(r '^api/4/news/latest$' , latest_news), url(r '^api/4/news/theme/(?P<theme_id>[0-9]+)/$' , theme_news), url(r '^api/4/news/(?P<news_id>[0-9]+)/$' , news_detail), ] |
括号包住的是要传入的参数,逗号后面的是匹配url成功后要执行的视图函数
括号里面有一些正则表达式,自己可以去百度搜一下怎么写
最后就可以把项目跑起来
效果如下