How can I Change type Of Django Calendar With Persian Calendar?

Ardalan Shahgholi picture Ardalan Shahgholi · Nov 29, 2013 · Viewed 8.3k times · Source

I use Django 1.6.

When I have a field that have DateTimeField type, Django automatically use Django calendar.

But in Iran we use Persian Calendar (or Jalali Calendar or Farsi Calendar).

How can I change Django auto generation because it generate Persian calendar in page?

In other hand, I want change default calendar with Persian Calendar?

enter image description here

Answer

Arman picture Arman · Jul 28, 2016

you can use django-jalali-date. Easy conversion of DateTimeFiled to JalaliDateTimeField within the admin site. only by inheritance a class!

settings.py

INSTALLED_APPS = [
    ...
    'jalali_date',
    ...
]

views.py

from jalali_date import datetime2jalali, date2jalali

def my_view(request):
     jalali_join = datetime2jalali(request.user.date_joined).strftime('%y/%m/%d _ %H:%M:%S')

template.html

{% load jalali_tags %}

<p>{{ request.user.date_joined|to_jalali:'%y/%m/%d _ %H:%M:%S' }}</p>

admin.py

from django.contrib import admin
from jalali_date.admin import ModelAdminJalaliMixin, StackedInlineJalaliMixin, TabularInlineJalaliMixin  

class MyInlines1(TabularInlineJalaliMixin, admin.TabularInline):
    model = SecendModel

class MyInlines2(StackedInlineJalaliMixin, admin.StackedInline):
    model = ThirdModel

@admin.register(FirstModel)
class FirstModelAdmin(ModelAdminJalaliMixin, admin.ModelAdmin):
    inlines = (MyInlines1, MyInlines2, )
    readonly_fields = ('some_fields', 'date_field',)

example of result!