How do I include image files in Django templates?

前端 未结 11 1521
时光说笑
时光说笑 2020-11-27 10:35

I\'m new to Django and I\'m trying to learn it through a simple project I\'m developing called \'dubliners\' and an app called \'book\'. The directory structure is like this

相关标签:
11条回答
  • 2020-11-27 11:06

    If you give the address of online image in your django project it will work. that is working for me. You should take a shot.

    0 讨论(0)
  • 2020-11-27 11:10

    Your

    <img src="/home/tony/london.jpg" />
    

    will work for a HTML file read from disk, as it will assume the URL is file:///home/.... For a file served from a webserver though, the URL will become something like: http://www.yourdomain.com/home/tony/london.jpg, which can be an invalid URL and not what you really mean.

    For about how to serve and where to place your static files, check out this document. Basicly, if you're using django's development server, you want to show him the place where your media files live, then make your urls.py serve those files (for example, by using some /static/ url prefix).

    Will require you to put something like this in your urls.py:

    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': '/path/to/media'}),
    

    In production environment you want to skip this and make your http server (apache, lighttpd, etc) serve static files.

    0 讨论(0)
  • 2020-11-27 11:13

    If your file is a model field within a model, you can also use ".url" in your template tag to get the image.

    For example.

    If this is your model:

    class Foo(models.Model):
        foo = models.TextField()
        bar = models.FileField(upload_to="foo-pictures", blank = True)  
    

    Pass the model in context in your views.

    return render (request, "whatever.html", {'foo':Foo.objects.get(pk = 1)})
    

    In your template you could have:

    <img src = "{{foo.bar.url}}">
    
    0 讨论(0)
  • 2020-11-27 11:14

    I do understand, that your question was about files stored in MEDIA_ROOT, but sometimes it can be possible to store content in static, when you are not planning to create content of that type anymore.
    May be this is a rare case, but anyway - if you have a huge amount of "pictures of the day" for your site - and all these files are on your hard drive?

    In that case I see no contra to store such a content in STATIC.
    And all becomes really simple:

    static

    To link to static files that are saved in STATIC_ROOT Django ships with a static template tag. You can use this regardless if you're using RequestContext or not.

    {% load static %} <img src="{% static "images/hi.jpg" %}" alt="Hi!" />

    copied from Official django 1.4 documentation / Built-in template tags and filters

    0 讨论(0)
  • 2020-11-27 11:15

    I have spent two solid days working on this so I just thought I'd share my solution as well. As of 26/11/10 the current branch is 1.2.X so that means you'll have to have the following in you settings.py:

    MEDIA_ROOT = "<path_to_files>" (i.e. /home/project/django/app/templates/static)
    MEDIA_URL = "http://localhost:8000/static/"
    

    *(remember that MEDIA_ROOT is where the files are and MEDIA_URL is a constant that you use in your templates.)*

    Then in you url.py place the following:

    import settings
    
    # stuff
    
    (r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
    

    Then in your html you can use:

    <img src="{{ MEDIA_URL }}foo.jpg">
    

    The way django works (as far as I can figure is:

    1. In the html file it replaces MEDIA_URL with the MEDIA_URL path found in setting.py
    2. It looks in url.py to find any matches for the MEDIA_URL and then if it finds a match (like r'^static/(?P.)$'* relates to http://localhost:8000/static/) it searches for the file in the MEDIA_ROOT and then loads it
    0 讨论(0)
提交回复
热议问题