I have this model:
class Event(models.Model):
title = models.CharField("Event Title",max_length=250)
private = models.BooleanField("Private event",default=False)
category = models.ForeignKey(Category)
created = models.DateTimeField(default=timezone.now')
This modelform:
class EventForm(forms.ModelForm):
private = forms.BooleanField(label='Private event',required=False)
class Meta:
model = Event
exclude = ('created',)
In my template the boolean field is not rendered. Even when I try to display the form with {{ form.as_p }}.
I have droped and created the database several times. I have checked permissions. I have checked migrations.
What am I missing?
In your forms.py add the widget as shown below:
private = forms.BooleanField(widget=forms.CheckboxInput, default=False)
This might help render the boolean field!
Your model field definitions for private and title should explicitly assign the text labels to the verbose_name keyword argument. I would also change from exclude to fields to see what happens when you list the set of fields and stop defining private on the form at all.
Related
I want to not to display the null value fields in template.So How can i achieve this in django?Is there any functions available to do this.
In you used Django ModeForm then do this
class ProductForm(forms.ModelForm):
class Meta:
model = ProductModel
exclude = ('selling_price','discounted_price') # this is exclude fields not render in html template
NOTE:- pass null=True, blank=True in Model
If what you are saying is that you do not want a field that has been set to null = True in your models.py to be rendered in your html template, you can use django inbulit form class to render the form and exclude whatever field is null
Here is an example of what i am saying.
class Userdetail(forms.ModelForm):
class Meta:
model = User_detail
exclude = ("name_of_null_field",) #This field won't be rendered in your html template because it is excluded.
You can read more on Django forms here Working with forms
I have a Django model that specifies the verbose_name and help_text; I want this information to be used in the Form for the model while also specifying additional information for the field in the form class.
The problem I'm having is that as soon as I add additional attributes to a form field I lose the verbose_name and help_text, meaning I have to specify them in the model and the form, which does not seem very DRY to me.
The model field is defined as:
class DataSubmission(models.Model):
...
upload_date = models.DateField(blank=True,
null=True,
help_text='Date of submission',
verbose_name='Upload date')
When the Form field is only defined in the Meta.fields the verbose_name appears as the form field label and the help_text can be accessed as well.
When as I define the Form as follows (the DatePicker widget is my own code):
class DataSubmissionForm(forms.ModelForm):
...
upload_date = forms.DateField(input_formats=settings.DATE_INPUT_FORMATS,
required=False,
widget=DatePicker)
...
class Meta:
model = DataSubmission
fields = (
...
'upload_date',
...
)
I lose the verbose_name and help_text from the model. How can I modify the form field without losing the information from the model?
My problems were caused by defining form fields as class variables, when I should be using the inner Meta class to modify the field behaviour.
I was probably following some tutorial or sample code I found when I defined the form fields in that way; but the documentation give me the answer (if only I'd read it carefully in the first place!):
To specify a custom widget for a field, use the widgets attribute of
the inner Meta class. This should be a dictionary mapping field names
to widget classes or instances.
That comment lead me to experiment with the other field settings I was using, by adding them to the Meta class.
So once I had converted the field declaration to:
class DataSubmissionForm(forms.ModelForm):
class Meta:
model = DataSubmission
fields = (
...
'upload_date',
...
)
input_formats = (
'upload_date': settings.DATE_INPUT_FORMATS,
)
required = (
'upload_date': False,
)
widget = (
'upload_date': DatePicker
)
The verbose_name and help_text became available again.
I am developing a site in django.I have to display a check box in templates.
I am using model form,
class ReportPersonForm(forms.ModelForm):
class Meta:
model = ReportPerson
fields = ['name','first_aid','sick_bay','ambulance']
I have to create check box for the following fields respectively,first_aid,sick_bay,ambulance and render in template.
Can anyone help me to create a check in django and how to design template to display the check box.
Thanks
If you define you field in model as BooleanFileld - you can use
{{ form.first_aid }}
in you template
In your forms file
class ReportPersonForm(forms.ModelForm):
first_aid = models.BooleanField()
sick_bay = models.BooleanField()
ambulance = models.BooleanField()
class Meta:
model = ReportPerson
fields = ['name','first_aid','sick_bay','ambulance']
Then for your template you have multiple options, read on in the docs what suits your needs:
Working with forms
I'm trying to make a CreateView have a readonly field with a set value, but I'm unable to make that work.
I have a model with a ForeignKey to another model:
class CompanyNote(TimeStampedModel):
company = models.ForeignKey(Company)
note = models.TextField(blank=True)
And I have a CreateView:
class CompanyNoteCreateView(CreateView):
model = models.CompanyNote
form_class = CompanyNoteForm
That uses a custom ModelForm:
class CompanyNoteForm(forms.ModelForm):
company = forms.ChoiceField(
widget=forms.widgets.Select(attrs={'readonly': 'readonly'}))
class Meta:
model = models.CompanyNote
As you see, the widget for the field in question is readonly. This is because I pick up the company as a part of the URL, as in company/1/note/add . I have no trouble picking up the "1" and finding the right company object, but I don't know how to set the readonly field.
I tried:
def get_initial(self):
initial = super(CompanyNoteCreateView, self).get_initial()
initial['company'] = self.get_company().id
return initial
But that didn't work. The Widget is empty, which may be the problem. Perhaps I'm barking up the wrong tree here. Any ideas welcome.
Have you tried setting the attribute in the Form's Meta class?
I experienced an issue where Form attributes were not applied for Model Fields if set in the base class definition, but they worked correctly in the Meta class:
class CompanyNoteForm(forms.ModelForm):
class Meta:
model = models.CompanyNote
widgets = {'company': forms.widgets.Select(attrs={'readonly': True,
'disabled': True})}
Otherwise check this answer out.
Worst case scenario, make company a hidden field?
Use a ModelChoiceField
class CompanyNoteForm(forms.ModelForm):
company = forms.ModelChoiceField(queryset=models.Company.objects.all(), widget=forms.widgets.Select(attrs={'readonly': 'readonly'}))
I could not find this answer anywhere, that I could actually get to work. But I found a different approach. Set the field to be hidden with forms.HiddenInput() widget. Then the value you pass in from the view will be assigned but the user cannot access it.
widgets = {'field_name': forms.HiddenInput()}
I'm using ModelForm class so my syntax might be different from yours.
class Label(models.Model):
name = ...
slug_name = ...
and here is the form I created straightly from model
class LabelForm(models.Model):
class Meta:
model = Label
How can I give url the widget HiddenInput? slug_name also cannot be blank because it is required...
Thanks
For the required field, use the blank field option with a TextField:
slug_name = TextField(blank=False)
For the HiddenInput widget, I assume there exists a url field in your model (which is not the case in the code you posted) check Django's own guide to overriding the default field types or widgets on a ModelForm:
class LabelForm(models.Model):
class Meta:
model = Label
widgets = {'url': HiddenInput()}