django-forms

How to add placeholder to forms of Django-Registration

旧巷老猫 提交于 2019-12-21 05:07:27
问题 I am using django-registration for my project. in my registration_form.html file: {{form.username}} {{form.email}} //other fields And I want to set placeholders for each field. But this is a kind of built-in app. so I need to find the way for editing these fields from my main app. I don't want to change source of django-registration. 回答1: If you can override the built-in form, you can define the placeholder as follows: class RegistrationForm(forms.ModelForm): class Meta: model = YourModelName

How to add placeholder to forms of Django-Registration

巧了我就是萌 提交于 2019-12-21 05:07:26
问题 I am using django-registration for my project. in my registration_form.html file: {{form.username}} {{form.email}} //other fields And I want to set placeholders for each field. But this is a kind of built-in app. so I need to find the way for editing these fields from my main app. I don't want to change source of django-registration. 回答1: If you can override the built-in form, you can define the placeholder as follows: class RegistrationForm(forms.ModelForm): class Meta: model = YourModelName

Django: Customizing a particular Form Field's HTML

旧街凉风 提交于 2019-12-21 04:23:12
问题 class ItemForm(forms.ModelForm): description = forms.CharField(label='Description', max_length=250, widget=forms.Textarea, required=False) image = forms.ImageField(label='Item Picture', max_length=50, required=False) start = forms.DateField(widget=SelectDateWidget, required=False) end = forms.DateField(widget=SelectDateWidget, required=False) cost_price = forms.CharField(label='Cost Price Per Unit', widget=???, max_length=5) class Meta: model = Item fields = ('image', 'name', 'description',

How do I make a Django ModelForm menu item selected by default?

你离开我真会死。 提交于 2019-12-21 03:56:20
问题 I am working on a Django app. One of my models, "User", includes a "gender" field, as defined below: GENDER_CHOICES = ( ('M', 'Male'), ('F', 'Female'), ) gender = models.CharField(max_length=1, choices=GENDER_CHOICES, null=True) I am using a ModelForm to generate a "new user" HTML form. My Google-fu seems to be failing me -- how can I make this HTML form have the "Male" item selected by default in the drop-down box? (i.e. so selected="selected" for this item.) 回答1: If you need a blank form

django init function with attrs in Form

余生长醉 提交于 2019-12-21 03:13:44
问题 im using Django Profiles, and i need to make some change in the profile form. In the normal way my form is fine completly, but now i would like a litter change, add jquery for repopulate a select form. The problem is that Django Profile make the form from the model, so i would like to add a attribute id to the select form (i did yet), but then the select dont show me the options (values), i dont understand why, the select form bring the value from another model with FK. Thanks guys :), Sorry

Django: Add non_field_error from view?

喜你入骨 提交于 2019-12-21 03:11:08
问题 I've got a Django form with a bunch of fields that I'm rendering in the template. I've also got some straight HTML input elements which I want to validate in the view by accessing the request.POST vars. If those don't validate, I want to inject an error into the Django form so I can display it on the page. Is there a way to do that? 回答1: You can also use quite elegant add_error() method. Works for Django >= 1.7. If you set field as None form will treat the error as "non_field" one. So: form

How to set default values in TabularInline formset in Django admin

心已入冬 提交于 2019-12-20 19:48:12
问题 How to set first default rows/values in django admin's inline? class Employee(models.Model): username = models.CharField(_('Username'), max_length=150, null=False, blank=False) email = models.CharField(_('Email'), max_length=150, null=False, blank=False) class Details(models.Model): employee = models.ForeignKey(Employee, verbose_name=_('Employee'), blank=False, null=False) label = models.CharField(_('Label'), max_length=150, null=False, blank=False) value = models.CharField(_('Value'), max

Django password problems

心不动则不痛 提交于 2019-12-20 19:41:29
问题 I'm using a modelform for User like so: class UserForm(forms.ModelForm): class Meta: model = User fields = ('username','password','email',) but the password field shows up as a regular textfield, not a password input. How do I make sure it shows a password field? I tried this: class UserForm(forms.ModelForm): username = forms.CharField(max_length = 15, min_length = 6) password = forms.PasswordInput() class Meta: model = User fields = ('username','password','email',) but that doesn't work

form with CheckboxSelectMultiple doesn't validate

混江龙づ霸主 提交于 2019-12-20 17:32:17
问题 I have a form with a choice field that is using CheckboxSelectMultiple widget: foo = forms.ChoiceField(widget=forms.CheckboxSelectMultiple, choices=( ("1", "ONE"), ("2", "TWO"), )) The form renders fine showing two checkboxes, however it doesn't validate. If I select both checkboxes I am getting an error: Select a valid choice. [u'1', u'2'] is not one of the available choices Selecting one checkbox doesn't work either, it gives me: Select a valid choice. [u'1'] is not one of the available

Django form field grouping

坚强是说给别人听的谎言 提交于 2019-12-20 12:36:10
问题 Say I have a form with 20 fields, and I want to put 10 of these fields (group1) in a particular div environment and the other 10 fields (group2) in a different div environment. Something like: <div class="class1"> {% for field in form.group1 %} {{ field.label}}: {{ field }} {% endfor %} </div> <div class="class2"> {% for field in form.group2 %} {{ field.label}}: {{ field }} {% endfor %} </div> Any ideas how I could accomplish this by iterating over the fields? More generally, I would like to