34π
β
The solution is:
class SettingsForm(forms.ModelForm):
company_logo = forms.ImageField(label=_('Company Logo'),required=False, error_messages = {'invalid':_("Image files only")}, widget=forms.FileInput)
class Meta:
model = Settings
fields = ("company_logo")
....
I added the widget forms.FileInput
, in order to tell the ImageField to use the basic field, not the one inherited from FileInput
.
π€0xmtn
9π
@mtndesign, you might also want a βremoveβ option, which you can place wherever you like in your template.
class MyForm(forms.ModelForm):
photo = forms.ImageField(required=False, widget=forms.FileInput)
remove_photo = forms.BooleanField(required=False)
...
def save(self, commit=True):
instance = super(MyForm, self).save(commit=False)
if self.cleaned_data.get('remove_photo'):
try:
os.unlink(instance.photo.path)
except OSError:
pass
instance.photo = None
if commit:
instance.save()
return instance
π€s29
- [Django]-Django check for any exists for a query
- [Django]-Best practices for getting the most testing coverage with Django/Python?
- [Django]-ImportError: No module named 'django.core.urlresolvers'
7π
You can change the widget used to render the form field by specifying it on initializing:
class SettingsForm(forms.ModelForm):
company_logo = forms.ImageField(label=_('Company Logo'),required=False, \
error_messages ={'invalid':_("Image files only")},\
widget=FileInput)
See the docs for widgets.
π€XORcist
- [Django]-React Proxy error: Could not proxy request /api/ from localhost:3000 to http://localhost:8000 (ECONNREFUSED)
- [Django]-How can I define a list field in django rest framework?
- [Django]-OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
Source:stackexchange.com