Django. URL, media img displays



  • pyhon 3django 1.8pillow fitted. There's a model. ImageField♪ We need to get a picture of the model on the page. I think I've got some sttings.

    tovar_img = models.ImageField(upload_to='uploads', default='/img/tovar.jpg')
    

    In the vein, I'm immersing all the objects from the model and transferring them as:

    {{ tovar.tovar_img }}
    

    Instead of paintings, they're on their way: /static/tovar.jpg and in a given ./tovar1.jpg

    Help me set up the sttings the way is right to download and download the pictures into one folder, and, accordingly, to display them.

    STATIC_URL = '/static/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'uploads')
    MEDIA_URL = '/uploads/'
    STATICFILES_DIRS = [ ('static', 'C:/Python34/mynewage/static'), ]
    


  • (1) {{ MEDIA_URL }} - it's part of the way to the pictures. http://site.com/MEDIA_URL/photo.jpg () On your website (not relevant to the data structure on the disk, i.e.ers). Ask MEDIA_URL, don't forget to add 'django.template.context_processors.media' contextual processors (context_processors)

    (2) {{ MEDIA_ROOT }} - it's a disk folder that keeps all the pictures that the user will download.

    (3) {{ STATIC_URL }} - in this context, it is irrelevant

    (4) tovar_img = models.ImageField(upload_to='uploads', default='/img/tovar.jpg') - with these structures, the photos will be kept in the folder of 'uploads' in MEDIA_ROOT (MEDIA_ROOT/uploads/filename.jpg - for example). Also in MEDIA_ROOT/img/tovar.jpg - you need to put this most default file.

    (5) The extraction shall be carried out in the template via img (e.g.): <img src="{{ tovar.tovar_img.url }}" alt="товар">

    (6) If you're developing a website (not for sale - for which there is nginx/apache), add it to the urls.py (so that dev-server could disperse the media files)

    from django.conf import settings
    from django.conf.urls.static import static
    # ...
    urlpatterns = [
        # ... 
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    (6) For content pictures, especially for Internet stores, I highly recommend https://github.com/mariocesar/sorl-thumbnail - allows for the giving of a certain size (containing the balance of the parties, which is important, as the content manager can shed a curve that would otherwise be removed from the borders/to be scaled up)




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2