django-forms

Adding css class to field on validation error in django

回眸只為那壹抹淺笑 提交于 2020-01-01 04:21:06
问题 I am using Django's modelform and its really good. How can I highlight the actual text box (e.g. border:red ) if there is a validation error associated with it. Basically what i want is to add a class (error) if there is a validation error to a field. 回答1: What about defining error_css_class? http://docs.djangoproject.com/en/dev/ref/forms/api/#styling-required-or-erroneous-form-rows? class MyForm(ModelForm): error_css_class = 'error' 回答2: Expanding upon errx's answer. Add the CSS .error input

Django: Overriding the clean() method in forms - question about raising errors

China☆狼群 提交于 2020-01-01 03:10:23
问题 I've been doing things like this in the clean method: if self.cleaned_data['type'].organized_by != self.cleaned_data['organized_by']: raise forms.ValidationError('The type and organization do not match.') if self.cleaned_data['start'] > self.cleaned_data['end']: raise forms.ValidationError('The start date cannot be later than the end date.') But then that means that the form can only raise one of these errors at a time. Is there a way for the form to raise both of these errors? EDIT #1 : Any

How do you dynamically hide form fields in Django?

断了今生、忘了曾经 提交于 2020-01-01 02:25:48
问题 I am making a profile form in Django. There are a lot of optional extra profile fields but I would only like to show two at a time. How do I hide or remove the fields I do not want to show dynamically? Here is what I have so far: class UserProfileForm(forms.ModelForm): extra_fields = ('field1', 'field2', 'field3') extra_field_total = 2 class Meta: model = UserProfile def __init__(self, *args, **kwargs): extra_field_count = 0 for key, field in self.base_fields.iteritems(): if key in self.extra

Django: How to access the display value of a ChoiceField in template given the actual value and the choices?

醉酒当歌 提交于 2020-01-01 02:00:09
问题 I have a ChoiceField in a bound form, whose choices are: [('all', 'All users'), ('group', 'Selected groups'), ('none', 'None')] In the template, I have no problem accessing its bound value (the actual value to be stored; the first element of each tuple) and the choices. With these pieces of info in hands, I know I should be able to access the display values, or the second element of each tuple. But how can I do that in the template? Thanks. 回答1: I doubt that it can be done without custom

Django : customizing FileField value while editing a model

送分小仙女□ 提交于 2019-12-31 22:25:31
问题 I've got a model, with FileField . When I edit this model in a view, I want to change the "current" value of FileField which gets displayed in the view form. Let me explain. models.py: class DemoVar_model(models.Model): ... Welcome_sound=models.FileField(upload_to='files/%Y/%m/%d') forms.py: class DemoVar_addform(ModelForm): ... class Meta: model = DemoVar_model views.py: soundform = DemoVar_addform(instance=ivrobj) .... return render_to_response(template,{'soundform':soundform}, ....) Now I

Django Imagefield not working properly via ModelForm

我怕爱的太早我们不能终老 提交于 2019-12-31 08:04:19
问题 I'm certain I'm doing something really obviously stupid, but I've been trying to figure it out for a few hours now and nothing is jumping out at me. I'm using a ModelForm so I can expose a few fields from a model for editing. 2x ImageField, 1x TextField. The Form is processed and the TextField works. The two ImageFields do not work and they're why I'm here today. I'm using Django 1.0.2 Here's the relevant code (ask if you need more -- and I'm not including the HTML because that part appears

Django Imagefield not working properly via ModelForm

爱⌒轻易说出口 提交于 2019-12-31 08:04:18
问题 I'm certain I'm doing something really obviously stupid, but I've been trying to figure it out for a few hours now and nothing is jumping out at me. I'm using a ModelForm so I can expose a few fields from a model for editing. 2x ImageField, 1x TextField. The Form is processed and the TextField works. The two ImageFields do not work and they're why I'm here today. I'm using Django 1.0.2 Here's the relevant code (ask if you need more -- and I'm not including the HTML because that part appears

How to render two form fields as one field in Django forms?

淺唱寂寞╮ 提交于 2019-12-31 05:58:08
问题 I'm working on Django application where I need to render a form differently. The form contains multiple fields related to a person. 1. Firstname 2. Lastname 3. Email 4. Address 5. City 6. Country 7. Phone 8. Pincode There are two flows in my application. In the first flow I can render all the form fields and save them when the user enters some data and submit. But in the second flow, I need to display only three fields as below. 1. Name - *Combination of Firstname and Lastname* 2. Email 3.

Validating a form with overloaded _init_

坚强是说给别人听的谎言 提交于 2019-12-31 05:34:30
问题 I have a form with a new init method, which allow to display various choices according to a parameter : class Isochrone_Set_Parameters(forms.Form): Grid_Choices = Grids_Selection.Grid_Choices def __init__(self, Grid_Type, *args, **kwargs): super(Isochrone_Set_Parameters, self).__init__(*args, **kwargs) if Grid_Type == Grids_Selection.Grid_Values[0]: Choices = (('0.0','0.0'),('0.1','0.1'),('0.3','0.3'),('0.5','0.5'),('0.6','0.6'),('0.7','0.7'), \ ('0.8','0.8'),('0.9','0.9'),('0.95','0.95'))

NoReverseMatch at /kitty_view Reverse for 'kitty' with arguments '(5,)' not found. 1 pattern(s) tried: ['kitty$']

天涯浪子 提交于 2019-12-31 05:31:30
问题 While rendering this kitty_view I am getting this error. Exactly the same thing I have copied from another app that is working properly. Kindly help. View.py def kitty_view(request): kitty_list = kitty.objects.all().order_by('-cretime') code1 = str(request.GET.get('Code')) name1 = str(request.GET.get('nam')) status1 = str(request.GET.get('stat')) if (name1 is not None and name1 != ''): kitty_list = kitty_list.filter(name=name1) if (code1 is not None and code1 != ''): kitty_list = kitty_list