how to show datepicker calendar on datefield

You can use widget to pass a class to the form when it will be rendered in the html. Then that class will be read by javascript to render the datepicker function. Here is an example: from django import forms class HolidayTimeForm(forms.Form): holiday_date = forms.DateField(widget=forms.TextInput(attrs= { ‘class’:’datepicker’ })) … or through the widgets attribute in … Read more

How to do a multi-step form in Django?

Of course there’s a way to do this in Django. One way is to hold your values in session until you submit them at the end. You can populate your forms using values held in session if you return to previous step. With some searching, you may find an app that someone has already written … Read more

Dropdown in Django Model

From model to template : models.py COLOR_CHOICES = ( (‘green’,’GREEN’), (‘blue’, ‘BLUE’), (‘red’,’RED’), (‘orange’,’ORANGE’), (‘black’,’BLACK’), ) class MyModel(models.Model): color = models.CharField(max_length=6, choices=COLOR_CHOICES, default=”green”) forms.py class MyModelForm(ModelForm): class Meta: model = MyModel fields = [‘color’] views.py class CreateMyModelView(CreateView): model = MyModel form_class = MyModelForm template_name=”myapp/template.html” success_url=”myapp/success.html” template.html <form action=”” method=”post”>{% csrf_token %} {{ form.as_p }} <input … Read more

How do you filter a nested serializer in Django Rest Framework?

You can subclass the ListSerializer and overwrite the to_representation method. By default the to_representation method calls data.all() on the nested queryset. So you effectively need to make data = data.filter(**your_filters) before the method is called. Then you need to add your subclassed ListSerializer as the list_serializer_class on the meta of the nested serializer. subclass ListSerializer, … Read more

Django ManyToMany model validation

It is not possible to do this validation in the model’s clean method, but you can create a model form which can validate the choice of words. from django import forms class SentenceForm(forms.ModelForm): class Meta: model = Sentence fields = [‘words’, ‘language’] def clean(self): “”” Checks that all the words belong to the sentence’s language. … Read more

Heroku app database resetting

As stated by @DanielRoseman you should not use SQLite on heroku, because SQLite runs in memory, and backs up its data store in files on disk. Let me quote from the heroku doku: SQLite runs in memory, and backs up its data store in files on disk. While this strategy works well for development, Heroku’s … Read more

AngularJS + Django Rest Framework + CORS ( CSRF Cookie not showing up in client )

AngularJS Single Page Web Application on Sub-domain A, talking to a Django JSON (REST) API on Sub-domain B using CORS and CSRF protection Since I’m currently working on a similar setup and was battling to get CORS to work properly in combination with CSRF protection, I wanted to share my own learnings here. Setup – … Read more