(Jul-04-2018, 09:53 AM)gontajones Wrote: Try this:
class Brand(models.Model): name = models.CharField(max_length=60) description = models.TextField(blank=True, null=True) image_url = forms.CharField(widget=forms.HiddenInput()) created_at = models.DateTimeField(auto_now_add=True, editable=False) updated_at = models.DateTimeField(auto_now=True) @property def logo(self, folder): logo_name_with_ext = '{}{}'.format(self.name, '.png') self.image_url = '{}/{}'.format(IMAGE_PATH, os.path.join(folder, logo_name_with_ext)) return mark_safe('<img src="{}" height="20" />'.format(logo_url)) class Meta: managed = False db_table = 'brand' def __str__(self): return self.name new_brand = Brand() new_brand.name = "Brand Name" new_brand.description = "Brand Description" html_img_tag = new_brand.logo('brand') new_brand.save()
I am unable to understand the relationship between image_url and logo here-
new_brand = Brand() new_brand.name = "Brand Name" new_brand.description = "Brand Description" html_img_tag = new_brand.logo('brand') new_brand.save()And if I add below line-
image_url = forms.CharField(widget=forms.HiddenInput())It says
Error:unresolved reference 'forms'
Once I change it to models, it says Error:AttributeError: module 'django.db.models' has no attribute 'HiddenInput'
For workaroung I movedd your changes in my forms.py as below-
class BrandForm(forms.ModelForm): logo = forms.ImageField(required=False) image_url = forms.CharField(widget=forms.HiddenInput()) class Meta: model = Brand fields = '__all__' def clean_logo(self): valid_extensions = ['.png', '.jpg', '.jpeg'] name = self.cleaned_data.get('name') logo = self.cleaned_data.get('logo') if logo: image_processor = ImageProcessor(logo) image_url = image_processor.upload(name, 'brand', valid_extensions) new_brand = Brand() new_brand.image_url = image_url new_brand.save() print('image_url is',image_url) return logoBut when I click on the save button, it says
Error:Please correct the error below
Neither in server nor in log file there is any error.