Kuracha
Kuracha

Reputation: 321

How I can store images in Database by using url to images

My problem is that I want to store images in database, but I don't want to store them physically in my project as static files, I want to save in database url to images which are already uploaded somewhere in Internet. How can I do this, just use CharField or TextField as type of field and paste url? But I also want later to display this photo in my template.

class Image(models.Model):
    name = models.CharField(max_length=25, verbose_name='Image name')
    image_file = 

    def __str__(self):
        return self.name

Upvotes: 0

Views: 244

Answers (1)

Ali Akhtari
Ali Akhtari

Reputation: 1287

If you want to use the external image source link in you django project, all you need is storing image source link in you model which you already did this in you models.py:

name = models.CharField(max_length=25, verbose_name='Image name')

But if consider this that you can't use google images source's link because of some limit, read this article to understand why :

What Happened To Google Image Search And Why You Can No Longer View Images Directly by the way you can use urlfiled instead of using charfiled, because it's only store urls. Django urlfield

after storing your image source link in your model, you can get it in you view the pass it too template and load the image in your template without downloading your image.

if you need any further help, ask and will happy to help.

Upvotes: 1

Related Questions