Multiple form classes in django generic (class) views

Facing similar problem, I’ve come to conclusion that it’s not possible. Though having multiple forms per page itself turned out to be a design mistake, presenting all sorts of troubles. E.g., user fills two forms, clicks submit on one of them and loses data from the other. Workaround requires complicated controller that needs to be … Read more

Get type of Django form widget from within template

Making a template tag might work? Something like field.field.widget|widget_type Edit from Oli: Good point! I just wrote a filter: from django import template register = template.Library() @register.filter(‘klass’) def klass(ob): return ob.__class__.__name__ And now {{ object|klass }} renders correctly. Now I’ve just got to figure out how to use that inside a template’s if statement. Edit … Read more

Django: access the parent instance from the Inline model admin

Django < 2.0 Answer: Use Django’s Request object (which you have access to) to retrieve the request.path_info, then retrieve the PK from the args in the resolve match. Example: from django.contrib import admin from django.core.urlresolvers import resolve from app.models import YourParentModel, YourInlineModel class YourInlineModelInline(admin.StackedInline): model = YourInlineModel def get_parent_object_from_request(self, request): “”” Returns the parent object … Read more

CSRF Token missing or incorrect

Update: This answer is from 2011. CSRF is easy today. These days you should be using the render shortcut function return render(request, ‘template.html’) which uses RequestContext automatically so the advice below is outdated by 8 years. Use render https://docs.djangoproject.com/en/2.2/topics/http/shortcuts/ Add CSRF middleware https://docs.djangoproject.com/en/2.2/ref/csrf/ Use the {% csrf_token %} template tag Confirm you see the CSRF … Read more

Django form.as_p DateField not showing input type as date

You can create a custom widget: from django import forms class DateInput(forms.DateInput): input_type=”date” class MyModelForm(forms.ModelForm): class Meta: model = MyModel fields=”__all__” widgets = { ‘my_date’: DateInput() } There’s no need to subclass DateInput. class MyModelForm(forms.ModelForm): class Meta: model = MyModel fields=”__all__” widgets = { ‘my_date’: forms.DateInput(attrs={‘type’: ‘date’}) }