S
S
slowkazak2014-12-10 15:01:29
Django
slowkazak, 2014-12-10 15:01:29

Why in django I have a 404 error instead of an image?

Good afternoon! Here is the thing, I upload the image to the server and also the link to the image in the database
And I prepare it for uploading in the admin panel in the form of a preview

def make_path(instance,filename,prefix=False):
    n1 = random.randint(0,10000)
    filename = str(n1)+".jpg"
    return u"%s/%s" % (settings.IMAGE_DIR, filename)

class Content(MPTTModel):
    image = models.ImageField(upload_to=make_path, default="",  blank=True,  verbose_name="Изображение")

    def pic(self):
        if self.image:
            return u'<img src="%s" width="170"/>' % self.image.url
        else:
            return '(none)'

In the admin panel, the result is given def pic (self)
7672.jpg
However, the picture is not loaded due to the lack of a file.
#settings.py
IMAGE_DIR = "img"

Project structure:
testsite
1) static
2) media
2.1) img
7672.jpg
The problem is that with Django and the Pitomy as a whole I'm still on you, so I admit that somewhere stupid flaws can be

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2014-12-10
@slowkazak

If on a local, then look in urls.py. There you need to give static.
djbook.ru/rel1.7/ref/contrib/staticfiles.html?high
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question