How to limit file types on file uploads for ModelForms with FileFields?

Create a validation method like: def validate_file_extension(value): if not value.name.endswith(‘.pdf’): raise ValidationError(u’Error message’) and include it on the FileField validators like this: actual_file = models.FileField(upload_to=’uploaded_files’, validators=[validate_file_extension]) Also, instead of manually setting which extensions your model allows, you should create a list on your setting.py and iterate over it. Edit To filter for multiple files: def … Read more

django model Form. Include fields from related models

A common practice is to use 2 forms to achieve your goal. A form for the User Model: class UserForm(forms.ModelForm): … Do stuff if necessary … class Meta: model = User fields = (‘the_fields’, ‘you_want’) A form for the Student Model: class StudentForm (forms.ModelForm): … Do other stuff if necessary … class Meta: model = … Read more

ModelForm with OneToOneField in Django

You have to create second form for PrinterAddress and handle both forms in you view: if all((profile_form.is_valid(), address_form.is_valid())): profile = profile_form.save() address = address_form.save(commit=False) address.printer_profile = profile address.save() Of course in the template you need to show both forms under one <form> tag 🙂 <form action=”” method=”post”> {% csrf_token %} {{ profile_form }} {{ address_form … Read more

Django, save ModelForm

You dont need to redefine fields in the ModelForm if you’ve already mentioned them in the fields attribute. So your form should look like this – class SelectCourseYear(forms.ModelForm): class Meta: model = Student fields = [‘course’, ‘year’] # removing user. we’ll handle that in view And we can handle the form with ease in the … Read more

How do I input HTML into the help text of a Django form field?

You can use mark_safe in the model to indicate the html is safe and it should be interpreted as such: from django.utils.safestring import mark_safe i_agree = forms.CharField(label=””, help_text=mark_safe(“Initial to affirm that you agree to the <a href=”https://stackoverflow.com/contract.pdf”>contract</a>.”), required=True, max_length=”4″)

How to clear form fields after a submit in Django

After saving form instead of showing post dict assign the empty form form = EmployeeForm() if request.method == “POST”: pDict = request.POST.copy() form = EmployeeForm(pDict) #if not valid shows error with previous post values in corresponding field if form.is_valid(): form.save() form = EmployeeForm() # show empty form no need to give HttpResponseRedirect()

How to set css class of a label in a django form declaration?

Widgets have an attrs keyword argument that take a dict which can define attributes for the input element that it renders. Forms also have some attributes you can define to change how Django displays your form. Take the following example: class MyForm(forms.Form): error_css_class=”error” required_css_class=”required” my_field = forms.CharField(max_length=10, widget=forms.TextInput(attrs={‘id’: ‘my_field’, ‘class’: ‘my_class’})) This works on any … Read more