Django

Django Model Serializer

  • Posted on: 5 April 2015
  • By: oon

How to serialize model to JSON on Django?

Django has serializers module on it's core.

from django.core import serializers 
from myproject.myapp import models 
data = serializers.serialize("json", models.MyModel.objects.all())

really simple :)

[1]http://stackoverflow.com/questions/1113096/django-dump-data-for-a-single...

Jinja2 syntax equivalent to Django's URL name and static

  • Posted on: 4 April 2015
  • By: oon
Django's template URL name like for Jinja2

(after start using Jinja2 on Django)

I need to use Django syntax {% url %} in Jinja2, here's my reference [1].

{{ url('index', args=[variable]) }}

And Django syntax {% static %} also similar.

{{ static('path') }}

hope it helps!

[1]https://medium.com/@samuh/using-jinja2-with-django-1-8-onwards-9c58fe1204dc

 

Install and Start Project Django

  • Posted on: 2 April 2015
  • By: oon

Install Django with pip package manager, for example inside (virtual) environment, install version 1.8 LTS:

pip install "django < 1.9"

You can check your install version (not only using pip also) with command django-admin

django-admin --version

 for example it will output: 1.8

Now we are ready to create Django project:

django-admin startproject rdjango

it will create directory rdjango, and inside the directory there will be these files:

Pages