django-forms

Passing parameters to inline form in django admin

有些话、适合烂在心里 提交于 2019-12-22 17:52:27
问题 I have an admin class inheriting from ModelAdmin: class TemplateAdmin (admin.ModelAdmin): inlines = (TemplateAttributeInline, CompanyAttributeInline) list_display = ("name", "created", "updated","departments") list_filter = ['companies__company'] list_editable = ("departments",) search_fields = ("name", "companies__company",) exclude = ("companies",) save_as = True I want to pass a parameter to TemplateAttributeInline which will in turn then pass the parameter to TemplateAttributeForm . What

Inline formset factory - pass request to child form

99封情书 提交于 2019-12-22 17:50:00
问题 I'm facing a quite challenging taks: I need an inlineformset_factory connecting my ParentEntity to my foreign key-bound ChildEntities . My ChildEntity contains a foreign key relation I need to filter per logged-in user - so I need the request in the ChildForm . What I've tried so far: I tried to use the form= kwarg but I can't pass an instance - just a class. So no way for me to add the request here. I tried to use the formset= kwarg but when I try to pass the request=request as a kwarg of

displaying django form error messages instead of just the field name

拟墨画扇 提交于 2019-12-22 13:05:55
问题 I have a form and I want to display the errors in a for loop. {% for error in form.errors %} <tr><td>{{ error }}</td></tr> {% endfor %} By doing this the {{ error }} only contains the field name that has an error, but not the error message. How can I display the error message? 回答1: You can get all field errors in a form like this: {% for field in form %} {{ field.errors|striptags }} {% endfor %} Or for a specific field: {% if form.subject.errors %} <ol> {% for error in form.subject.errors %}

How to create double and long text field from django models

佐手、 提交于 2019-12-22 11:15:37
问题 How to create a double field in mysql using django models. i run into this error and also how to create a longtext datatype with django class Test(models.Model): maxcount = models.DoubleField(null=True) ## double DEFAULT NULL, cs = models.TextField()#longtext, Error: maxcount = models.DoubleField(null=True) ##double DEFAULT NULL, AttributeError: 'module' object has no attribute 'DoubleField' 回答1: https://docs.djangoproject.com/en/1.8/ref/models/fields/#bigintegerfield https://docs

Django crispy-forms cannot find CSS

只愿长相守 提交于 2019-12-22 11:01:21
问题 I am using Django and Crispy Forms. I can get the form to render correctly, but no CSS formatting appears. What do I need to do? I have added CRISPY_TEMPLATE_PACK = 'bootstrap' to my settings.py file. The html file is as simple as it gets: {% extends 'base.html' %} {% load crispy_forms_tags %} {% block content %} {% crispy form %} {% endblock %} What else is necessary to make it work? I understand that since the bootstrap files come bundled with crispy_forms, I don't need to copy and

Django admin site: prevent fields from being edited?

我怕爱的太早我们不能终老 提交于 2019-12-22 10:45:40
问题 is it possible to prevent certain fields to be edited after they've been saved? They should be editable when the user creates a new item of a certain model but then when they try to open them to edit certain fields are 'blocked'. thanks 回答1: You could override your ModelAdmin's get_readonly_fields to set certain fields readonly: class MyAdmin(admin.ModelAdmin): def get_readonly_fields(self, request, obj=None): if obj: # when editing an object return ['field1'] return self.readonly_fields 来源:

Using Django FormPreview the right way

ぐ巨炮叔叔 提交于 2019-12-22 10:39:56
问题 My Goal I have a django project with a form, and I want to display a preview page before the user submits. The problem I can display a preview page using a Django FormPreview, but not all form data is displayed properly. Specifically, if I have a field with choices , the string values of these choices aren't displayed. I'm also having problems applying template filters to date fields. The end result is that some data on the preview page is visible but other data is blank: However, if I

How to save inline formset models in Django?

青春壹個敷衍的年華 提交于 2019-12-22 10:35:06
问题 Formsets have a .save() method, and the documentation says to save in views like this: if request.method == "POST": formset = BookInlineFormSet(request.POST, request.FILES, instance=author) if formset.is_valid(): formset.save() # Do something. else: formset = BookInlineFormSet(instance=author) I am following this, and it works when the parent is created, but I'm getting an exception in Django when it is saving existing models. The parent actually is saved to the database and the exception

Django: Save Form Data to Database

房东的猫 提交于 2019-12-22 10:30:43
问题 I'm using a form in my Django project to get people's name and number for later contact. Followed several guides including the Django docs but it doesn't seem to work. My test POST doesn't appear in admin and it can't be accessed via the manage.py shell either. I left in the multi-line comment in my code to show another method I was trying just in case. Getting no errors thrown in my manage.py runserver logs either. Form: from django import forms class SubscribeForm(forms.Form): name = forms

django forms client side validation?

浪尽此生 提交于 2019-12-22 10:27:04
问题 Is there a library/app which will help to easily implement a client side validation in django ? I found many form layout/display libraries like django-uni-form django-crispy-forms etc but none of them have a client side validation. Can any one point to a good framework. 回答1: Check out django-helpers (I am the author of the library). There is a form renderer app which will generate the form in a easily fancy bootstrap template and also implements client side validations for some validators