Python Forum
How to save uploaded image url in database from Django?
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to save uploaded image url in database from Django?
#11
(Jul-04-2018, 11:18 AM)gontajones Wrote: I made a mistake.

In your model the field must be something like:
image_url = models.CharField(max_length=200)
Why are you doing this?
image_url = forms.CharField(widget=forms.HiddenInput())
image_url = '' # <- This

I have removed this line-
image_url = ''

So now I have changed in models.py like below-
class Brand(models.Model):
    name = models.CharField(max_length=60)
    description = models.TextField(blank=True, null=True)
    image_url = models.CharField()
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    updated_at = models.DateTimeField(auto_now=True)

    @property
    def logo(self):
        return get_logo(self.name, 'brand')

    class Meta:
        managed = False
        db_table = 'brand'

    def __str__(self):
        return self.name

new_brand = Brand()
new_brand.image_url = new_brand.logo('brand')
new_brand.save()
In this line-
new_brand.image_url = new_brand.logo('brand')
I am getting error-
Error:
new_brand.image_url = new_brand.logo('brand') TypeError: 'SafeText' object is not callable
Reply
#12
What about this:

class Brand(models.Model):

    name = models.CharField(max_length=60)
    description = models.TextField(blank=True, null=True)
    image_url = models.CharField()
    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()
Reply
#13
same error-
Error:
html_img_tag = new_brand.logo('brand') TypeError: 'SafeText' object is not callable
Reply
#14
I think that the @property is causing this.
I can't test it right now. Later I'll test it to figure out how can we do this.
Reply
#15
I have solved the issue by my own and sharing here for others-
def save(self,commit=True):
        brand = super(BrandForm, self).save(commit=False)
        brand.image_url = self.image_url
        brand.save()
        return brand
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Django: View is unable to find attributes of database model pythonpaul32 0 459 Dec-07-2023, 06:38 PM
Last Post: pythonpaul32
  Save JSON data to sqlite database on Django Quin 0 2,790 Mar-26-2022, 06:22 PM
Last Post: Quin
Question wkhtmltoimage generated jpeg save to Database using mongoengine Madoo 2 3,008 Aug-18-2020, 03:42 PM
Last Post: Madoo
  SHow image in a static link of Django pycada 3 2,486 Mar-04-2020, 01:50 AM
Last Post: pycada
  Send email to gmail after user fill up contact form and getting django database updat Man_from_India 0 2,059 Jan-22-2020, 03:59 PM
Last Post: Man_from_India
  how retrieve database save multiple data in web Django 2.1 taomihiranga 0 2,752 Jul-30-2019, 04:58 PM
Last Post: taomihiranga
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 3,217 Jun-30-2019, 12:21 AM
Last Post: scidam
  Read Save RadioButtons from Database in Python Flask Webpage Gary8877 0 7,086 Apr-11-2019, 12:33 AM
Last Post: Gary8877
  Django- Remove leading zeros in values from database ntuttle 1 3,441 Mar-07-2019, 07:30 PM
Last Post: nilamo
  how i save the html form to flask database mebaysan 1 7,227 Feb-07-2019, 12:56 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020