django-forms

Pass json and deserialize form in django

你离开我真会死。 提交于 2019-12-21 17:45:11
问题 The issue is next: I send some post data with ajax to server. This data looks like: data = { form : $(this).serialize(), some_array: [2,3,4,1] } How to get form object in django? request.POST['form'] returns a string with a form. I'm trying to use import json library. But, when I run value = json.load(request.POST['some_array']) or form = json.load(request.POST['form']) it doesn't work. Printing request.POST['form'] returns the following: u'csrfmiddlewaretoken=I3LWAjfhZRGyd5NS3m9XcJkfklxNhxOR

Django forms - append to class meta exclude and widgets

泄露秘密 提交于 2019-12-21 13:10:23
问题 Is it possible to append to the exclude or widgets variables of an inherited Form? I have the following set up so far. class AddPropertyForm(forms.ModelForm): num_months = forms.ChoiceField(choices=MONTHS) request_featured = forms.BooleanField(required=False) featured_months = forms.ChoiceField(choices=MONTHS) class Meta(): model = RentalProperty exclude = ('listing_id', 'active_listing', 'active_listing_expiry_date', 'featured_property', 'featured_expiry_date', 'slug', 'property_manager')

How does one use a custom widget with a generic UpdateView without having to redefine the entire form?

时光总嘲笑我的痴心妄想 提交于 2019-12-21 07:59:08
问题 I have a model with a ManyToMany relation that I would like to update with a CheckBoxSelectMultiple widget while everything else uses the default generic form, but when I redefine that one form field, it is the only one that shows up in the UpdateView. Is there a way to use a widget with just one field without having to redefine the entire form? Views.py: from django.views.generic.edit import UpdateView from kunden.models import Kunde, Unternehmenstyp from kunden.forms import KundeEditForm

Blank option in required ChoiceField

我怕爱的太早我们不能终老 提交于 2019-12-21 07:41:04
问题 I want my ChoiceField in ModelForm to have a blank option (------) but it's required. I need to have blank option to prevent user from accidentally skipping the field thus select the wrong option. 回答1: This works for at least 1.4 and later: CHOICES = ( ('', '-----------'), ('foo', 'Foo') ) class FooForm(forms.Form): foo = forms.ChoiceField(choices=CHOICES) Since ChoiceField is required (by default), it will complain about being empty when first choice is selected and wouldn't if second. It's

Django Inline Formsets using custom form

微笑、不失礼 提交于 2019-12-21 07:25:15
问题 I am using inline formsets. My model: class Author(models.Model): description = models.CharField(max_length=100) class Book(models.Model): author = models.ForeignKey(Author) details = models.CharField(max_length=100) class AuthorForm(ModelForm): class Meta: widgets = { 'description': Textarea(attrs={'cols': 40, 'rows': 4}), } In my views.py I made the form from the AuthorForm like so form = AuthorForm(request.POST) But I also made a formset for the Books InlineFormSet = inlineformset_factory

Building a formset dynamically

戏子无情 提交于 2019-12-21 06:35:05
问题 I initially wrote code to build a form dynamically, based on data from the DB, similar to what I described in my previous SO post. As SO user Daniel Roseman points out, he would use a formset for this, and now I've come to the realization that he must be completely right. :) My approach works, basically, but I can't seem to get validation across the entire form to be working properly (I believe it's possible, but it's getting quite complex, and there has to be a smarter way of doing it =>

Building a formset dynamically

笑着哭i 提交于 2019-12-21 06:34:10
问题 I initially wrote code to build a form dynamically, based on data from the DB, similar to what I described in my previous SO post. As SO user Daniel Roseman points out, he would use a formset for this, and now I've come to the realization that he must be completely right. :) My approach works, basically, but I can't seem to get validation across the entire form to be working properly (I believe it's possible, but it's getting quite complex, and there has to be a smarter way of doing it =>

Django: ajax response for valid/available username/email during registration

三世轮回 提交于 2019-12-21 06:18:42
问题 I am using jQuery to do some inline form validation during user registration to prevent form errors after posting by checking to see if: username is available email has not already been registered The idea is to give the user feedback before the form is submitted to prevent frustration. The code is at the bottom. Questions: Is this a potential security problem? I had the thought that someone looking at my javascript could find the url I am polling for the username/email confirmation and then

django form: how to check out checkbox state in template

为君一笑 提交于 2019-12-21 05:33:14
问题 I have a form with checkboxes that works fine, but when the user submits the form with errors and my checkbox is checked I need to change div class which holds the checkbox. So I was experimenting with {{ form.mycheckbox.data }} in the template and it says False on page load. Now when user is click on a checkbox and the form has errors, it says on . So I tried: {{ if form.mycheckbox.data == True }} doesn't work {{ if form.mycheckbox.data != False }} doesn't work {{ if form.mycheckbox.data ==

Django: How to carry model form data from one page to another, and back, without commiting to the DB?

∥☆過路亽.° 提交于 2019-12-21 05:31:47
问题 [Preamble: Whereas I realize there may be simpler ways to do this (i.e., just use Django built-in Admin, or use inlines to do all editing on one page, etc.), unfortunately, I am not in control of the designs, so I'm seeking help on how to work with what I've been tasked with implementing.] I have 2 models, Deck and Slide. Slide has foreign key to Deck. (there is also an intermediate model--its a m2m relationship--but to simplify an already complex discussion, I'm going to pretend its a