How can I Change type Of Django Calendar With Persian Calendar?
Asked Answered
L

4

26

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

Lonne answered 29/11, 2013 at 9:42 Comment(1)
How did u manage it? I wanna do the same thing.Supernumerary
D
10

You will likely have to implement your own custom widget - I do not think Django provides a Persian calendar widget at this point in time.

There is an available code snippet for a Persian DateTime widget that I was able to locate, but have not yet tested. If it is not a perfect fit, then hopefully it will aid you in writing your own solution.

Dustheap answered 29/11, 2013 at 19:28 Comment(0)
P
14

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!

Popele answered 27/7, 2016 at 22:39 Comment(4)
please explain how to use this app in model, views and templatesKho
@Popele could you please explain how to implement a date field in model via this package. I want to know how to store posted value in database as a date or datetime field.X
it is only a forked project, main project is django-jallaliShoreless
Did you look at the codes of both projects? You judged hastily I used the jdatetime, no django-jalali @eisanahardaniPopele
V
12

Just to add to the accepted answer, the Jalali DateField, with its appropriate date picker can also be implemented without the use of any custom widget.

First you must specify your DateField to accept its input in the Jalali format. This can be achieved through this custom model field :

from django_jalali.db import models as jmodels

class Order(models.Model):
delivery_date =  jmodels.jDateTimeField(verbose_name='تاریخ تحویل')

Now, for displaying the date picker to the user, you can use any desired javascript date picker that suits your needs, something like This Bootstrap Datepicker.
Assuming the field is going to be displayed as part of a model form, you can specify the HTML id or class of the widget responsible for rendering it:

class Order(forms.ModelForm):

class Meta:
    model = Order
    widgets = {
        'delivery_date': forms.DateInput(attrs={'id':'datepicker'}),
    }

Finally in your template, you only need to initialize the date picker:

    <script>
    $(document).ready(function() {
        $("#datepicker").datepicker({
            minDate: 2,
            maxDate: "+10D",
            isRTL: true
        });

    });
   </script>

Here is how it would look like in the end:
Jalali Date Picker

Vierra answered 15/4, 2015 at 13:28 Comment(1)
The JDateField seems to use Gregorian DateField's validation. It causes a problem on 31th of second months. Any solutions for that?Grandparent
D
10

You will likely have to implement your own custom widget - I do not think Django provides a Persian calendar widget at this point in time.

There is an available code snippet for a Persian DateTime widget that I was able to locate, but have not yet tested. If it is not a perfect fit, then hopefully it will aid you in writing your own solution.

Dustheap answered 29/11, 2013 at 19:28 Comment(0)
O
3

If you just need Persian DateField (for example for birthday), this tiny code will do it

YEAR_CHOICES = range(1377, 1300, -1)
MONTH_CHOICES = {1: 'فروردین',2: 'اردیبهشت',3: 'خرداد',4: 'تیر',5: 'مرداد',6: 'شهریور',7: 'مهر',8: 'آبان',9: 'آذر',10: 'دی',11: 'بهمن',12: 'اسفند'}

class ProfileUpdate(forms.ModelForm):
    class Meta:
        model = Profile
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        super(ProfileUpdate, self).__init__(*args, **kwargs)
        self.fields['birthday'] = forms.DateField(required=False, widget=extras.SelectDateWidget(empty_label=['سال', 'ماه', 'روز'], years=YEAR_CHOICES, months=MONTH_CHOICES))
Otey answered 30/4, 2017 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.