0

New feature for output post with json format for Django Blog Python Learning v2.1.1

1. Add new function in your blog/models.py to outputing post with json format. (for complete script, checkout this: https://github.com/agusmakmun/Django-Blog-Python-Learning-v.2/commit/96657b82ab1dd48c74bfdf9c1af2bb12e8b07f6d)

def json_default_post(self):
    domain = 'https://python.web.id'
    cover_url = ''
    if self.cover.url:
        cover_url = self.cover.url
    else:
        cover_url = cover_url
    return dict(
            title = self.title, 
            url = domain+"/blog/"+str(self.slug), 
            cover =  domain+str(self.cover.url),
            author = self.author.name, 
            created = self.created.isoformat(), 
            modified = self.modified.isoformat(), 
            tags = [p.slug for p in self.tags.all()], 
            body = self.body
        )

2. Create new file with name json_format.py in folder of blog. (See this Changelog: https://github.com/agusmakmun/Django-Blog-Python-Learning-v.2/commit/5bb56b4b5d08791c7d4b67fc361e1f0a8e966c99)
for tree is like this:

___project/
________/blog/
____________/json_format.py
____________/models.py
____________/views.py
____________/urls.py

This simply how to outputing your models with json format. with exception if pk or id of post is does not exist. you can use with serialization formats json or something else, for more you can use rest framework, or checkout this docs with https://docs.djangoproject.com/en/1.9/topics/serialization/#serialization-formats-json

import json
from . import models
from django.http import HttpResponse
from django.core.exceptions import ObjectDoesNotExist

"""Outputing post to JSON format, with pk/id from post."""
def json_default_posts(request, pk):
    try:
        entry = models.Entry.objects.get(pk=pk)
        data = entry.json_default_post()
    except ObjectDoesNotExist:
        data = {'error_message':'Object Does NotExist'}

    return HttpResponse(json.dumps(data, indent=4), content_type='application/json')

3. Adding New url in your blog/urls.py for outputing post with json format. (See this changelog: https://github.com/agusmakmun/Django-Blog-Python-Learning-v.2/commit/778a01187723c69104bb7086c6cf2b10b9bcf22b)

from django.conf.urls import include, url
from django.conf.urls.static import static
from . import views

urlpatterns = [
    ....
    #outputing post with json format
    url(r'^json-posts/(?P<pk>S+)/$', json_format.json_default_posts, name="page_json_default_post"),
]

So, then if you call that url with <pk/id> of post, that will create post output with json format.

4. Calling url with name of url json-post and input pk with object.pk –> this pk of post. for example like this, in your templates/post.html (See this changelog: https://github.com/agusmakmun/Django-Blog-Python-Learning-v.2/commit/dea3798c27f90a0d8d885ceeeb2adf920e559d89)

<a class="btn-sm btn-warning" href="{% url 'page_json_default_post' pk=object.pk %}" target="_blank">JSON Output</a>

Hopefully usefull…

Update Bugs, 30-12-2015:

We have an error if posts have no covers, we recommend you replace with the latest this, in function of json_default_post at your models.py

def json_default_post(self):
    domain = 'https://python.web.id'
    cover_url = 'None'
    try:
        cover_url = domain+str(self.cover.url)
    except:
        cover_url = cover_url

    return dict(
            title = self.title, 
            url = domain+"/blog/"+str(self.slug), 
            cover =  cover_url,
            author = self.author.name, 
            created = self.created.isoformat(), 
            modified = self.modified.isoformat(), 
            tags = [p.slug for p in self.tags.all()], 
            body = self.body
        )

For more, you can checkout on this history: https://github.com/agusmakmun/Django-Blog-Python-Learning-v.2/commit/4736707d43d00ba10468f5aa314d974813cbf14a

Helpfully can help…

python django

Your Answer

blog comments powered by Disqus