Accessing “Media” files in Django

前端 未结 7 2225
日久生厌
日久生厌 2020-12-02 03:54

I\'d like to love Django, but this business of static and media files in development environments is driving me nuts. Please rescue me from my stupidity.

I\'m on my

7条回答
  •  难免孤独
    2020-12-02 04:44

    Here is another alternative:

    Set your media configs something like this inside 'settings.py':

    #Set media path
    MEDIA_ROOT = os.path.join(BASE_DIR,'media')
    MEDIA_URL = '/media/'
    

    Lets say I have a modal called person with image filed like below:

    class Person(models.Model):
        name = models.CharField(max_length = 30)
        photo = models.ImageField(upload_to = 'photos')
    

    Now here upload_to path we are taking about is inside the MEDIA_ROOT folder. In my case a media folder will be created inside which photos folder will be created and our media here will be dumped.

    So now in my template I do something like this:

    
                                                            
提交回复
热议问题