正在一步步完善墨颀 CMS,在暂时不考虑其他新的功能的时候,先和自己的博客整合一下。
之前用AngluarJS做的全部文章的时候是Tastypie做的API,只是用来生成的是博客的内容。只是打开的速度好快,可以在1秒内打开,献上URL:
http://www.phodal.com/api/v1/url/?offset=0&limit=20&format=json
之前只是拿Tastypie生成一些简单的JSON数据,如keywords_string,slug,title这些简单的数据。
因为这里的Blogpost是来自mezzanine,原来的api.py
,如下所示:
from tastypie.resources import ModelResource from mezzanine.blog.models import BlogPost, BlogCategory class AllBlogSlugResource(ModelResource): class Meta: queryset = BlogPost.objects.published() resource_name = "url" fields = ['keywords_string', 'slug', 'title'] allowed_methods = ['get'] class BlogResource(ModelResource): class Meta: queryset = BlogPost.objects.published() resource_name = "blog" fields = ['keywords_string', 'slug', 'title', 'content', 'description'] allowed_methods = ['get']
而这时为了测试方便,还需要解决跨域请求的问题,生成的内容大致如下所示:
{ "meta": { "limit": 1, "next": "/api/v1/url/?offset=1&limit=1&format=json", "offset": 0, "previous": null, "total_count": 290 }, "objects": [ { "keywords_string": "jquery backbone mustache underscore siderbar", "resource_uri": "/api/v1/url/369/", "slug": "use-jquery-backbone-mustache-build-mobile-app-cms-add-jquery-plugins", "title": "构建基于Javascript的移动web CMS——添加jQuery插件" } ] }
于是网上搜索了一下,有了下面的代码:
from tastypie.resources import Resource, ModelResource from mezzanine.blog.models import BlogPost, BlogCategory from django.http.response import HttpResponse from tastypie.exceptions import ImmediateHttpResponse from tastypie import http from tastypie.serializers import Serializer class BaseCorsResource(Resource): def create_response(self, *args, **kwargs): response = super(BaseCorsResource, self).create_response(*args, **kwargs) response['Access-Control-Allow-Origin'] = '*' response['Access-Control-Allow-Headers'] = 'Content-Type' return response def post_list(self, request, **kwargs): response = super(BaseCorsResource, self).post_list(request, **kwargs) response['Access-Control-Allow-Origin'] = '*' response['Access-Control-Expose-Headers'] = 'Location' return response def method_check(self, request, allowed=None): if allowed is None: allowed = [] request_method = request.method.lower() allows = ','.join(map(lambda s: s.upper(), allowed)) if request_method == 'options': response = HttpResponse(allows) response['Access-Control-Allow-Origin'] = '*' response['Access-Control-Allow-Headers'] = 'Content-Type' response['Access-Control-Allow-Methods'] = "GET, PUT, POST, PATCH" response['Allow'] = allows raise ImmediateHttpResponse(response=response) if not request_method in allowed: response = http.HttpMethodNotAllowed(allows) response['Allow'] = allows raise ImmediateHttpResponse(response=response) return request_method class AllBlogSlugResource(BaseCorsResource, ModelResource): class Meta: queryset = BlogPost.objects.published() resource_name = "url" fields = ['keywords_string', 'slug', 'title'] allowed_methods = ['get'] serializer = Serializer() class BlogResource(BaseCorsResource, ModelResource): class Meta: queryset = BlogPost.objects.published() resource_name = "blog" fields = ['keywords_string', 'slug', 'title', 'content', 'description'] allowed_methods = ['get'] serializer = Serializer()
接着便可以很愉快地、危险地跨域。
接着修改了一下代码中configure.json的blogListUrl,以及模块
<div class="l-box blogPosts"> <h2>动 态</h2> {{#objects}} <p> <!--<span class="date">{{created}}</span>--> <a href="#/blog/{{slug}}" alt="{{title}}">{{title}}</a> </p> {{/objects}} </div>
便可以请求到结果了。
一开始对于可配置的选择是正确的.
代码见: https://github.com/gmszone/moqi.mobi/tree/django
QQ讨论群: 344271543
源码 Github: https://github.com/gmszone/moqi.mobi