Forums

Having trouble displaying images for a blog using django (I'm a beginner)

*Can someone help me understand how to upload static file( images specifically) into a website using django v. 2.0. My codes are as below and I keep getting a broken image icon on my website *

My settings.py

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))

TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')

STATIC_DIR = os.path.join( BASE_DIR , 'static' )

STATIC_URL = '/static/'

STATICFILES_DIR = [ STATIC_DIR, ]

STATIC_ROOT = STATIC_DIR

My index.html file(not the whole of it)

<!DOCTYPE html>

{% load staticfiles %}

img src="{% static 'images/volcano.jpg' %}" height="400" width="400"

The path to my image is

C:\Users\user\Envs\website\website\static\images\volcano.jpg

My urls.py file is

from django.contrib import admin

from django.urls import path, include

from blog.views import home

urlpatterns = [

path('admin/', admin.site.urls),

path('',home, name='home'),

path('blog/', include('blog.urls')),

]

My views.py

from django.shortcuts import render

from django.http import HttpResponse

def home(request):

context_dict = {'boldmessage': "Crunchy, creamy, cookie, candy, cupcake!"}

return render(request,'rango/index.html',context=context_dict)

See our documentation about static files: http://help.pythonanywhere.com/pages/DjangoStaticFiles/ and http://help.pythonanywhere.com/pages/DebuggingStaticFiles/