2017-05-14 2 views
0

meine Einstellungsdatei wie unten ist,Django-Image-Datei rendert nicht

STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(BASE_DIR,'static') 
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static', 'static_dirs'),) 

MEDIA_URL = '/static/images/' 
MEDIA_ROOT = '/Users/bhargavsaidama/5ai/source/static/images/' 

#MEDIA_ROOT = os.path.join(BASE_DIR,'static', 'images') (tried this too) 

meine HTML-Lade Seite wie unten ist, Anmerkung: Ich direkt bin mit dem Dateipfad hier

index.html:

<!DOCTYPE html> 
{% load staticfiles %} 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <meta name="description" content=""> 
    <meta name="author" content=""> 

meine eigentliche hTML-Datei ist:

{% extends "index.html" %} 


{% block content %} 

<div class="content"> 

    <img src ='/Users/bhargavsaidama/5ai/source/5ai/static/images/Indian-economy.jpg' alt="My image"> 

    <h2 id = "title"><font face = "Comic sans MS"> {{ post.title }} </font></h2> 

    {% for sub_text in post.content %} 

    <p id = "data"><font face = "Comic sans MS" size="+0.3"> {{ sub_text }} </font></p> 

    {% endfor %} 

</div> 
{% endblock %} 

noch habe ich versucht, mit:

<img src ='Indian-economy.jpg' alt="My image"> 

.... aber kein Glück

aber die Ausgabe ist enter image description here

, wenn ich mit einer normalen HTML-Datei versuchen sagen läßt:

<html> 
<p> this is bhargav sai</p> 
<img src= '/Users/bhargavsaidama/5ai/source/5ai/static/images/Indian-economy.jpg' alt = 'my image'> 
</html> 

Der Ausgang ist: enter image description here

Sogar meine direkte URL aus lokaler Host konnte das Bild holen: enter image description here

kann jemand mir auf diesem helfen?

+0

Sie sollten nicht absolute Pfade für 'MEDIA_ROOT' verwenden, genau wie' STATICFILES_DIRS' – karthikr

+0

@karthikr, auch wenn ich os.path.join (BASE_DIR, 'statisch', 'images') als mein Media-Root verwende. Das Ergebnis ist das gleiche. Das ist der Grund, warum ich es mit absolutem Pfad versuchen musste. – Bhargav

+0

Wie sieht das gerenderte HTML aus? – karthikr

Antwort

1

sollten Sie besser wissen, wie statische Dateien zu konfigurieren und laden, siehe official doc.

Als Ihr jpg in static/images Ordner ist, nur Ihre tatsächliche html wie folgt ändern:

{% extends "index.html" %} 


{% block content %} 

{% load static %} #load static directory 
<div class="content"> 

    #load your static image 
    <img src ='{% static "images/Indian-economy.jpg" %}' alt="My image"> 

    <h2 id = "title"><font face = "Comic sans MS"> {{ post.title }} </font></h2> 

    {% for sub_text in post.content %} 

    <p id = "data"><font face = "Comic sans MS" size="+0.3"> {{ sub_text }} </font></p> 

    {% endfor %} 

</div> 
{% endblock %} 

Dieser Wille arbeite für deinen Fall.

+0

eigentlich. Die Magie ist in DEBUG = TRUE/FALSE – Bhargav

0

Wenn Sie von Google kommen, Behalten Sie Ihren DEBUG-Status in Ihrer Einstellungsdatei im Auge. Der Zugriff auf Mediendateien im dev-Server und -Prod-Server ist etwas anders. Im prod Server müssen sie mit Ihrem Webserver (apache/nginx) konfiguriert werden. überprüfen Sie bitte die official Doc.