[Answered ]-How to take a pre-designed webpage to django and add my own static files

0👍

✅

So This blog made it all Crystal clear and with these settings I could make it works

setting.py

STATIC_URL = '/static/'
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATICFILES_DIRS = (
    os.path.join(PROJECT_PATH, 'static_assets'),
) 

index.html

{% load static from staticfiles %}
<img src="{% static 'img/author.jpg' %}" alt="My image"/>

Folder Structure

mysite--
    |
    mysite
    |     |
    |     static
    |      |
    |      my css js files
    |
    static_assets
    |      
    templates
        |
        index.html

2👍

You are nearly there, but you shouldn’t be putting the other resources (JS, CSS, images) into the templates folder. That’s only for templates! You need to read the static files docs:

https://docs.djangoproject.com/en/1.7/howto/static-files/

This is needed to ensure proper separation of your code (which includes templates – they are a kind of server-side code) and your resources.

(If you come from a PHP background, you might be frustrated by this, because it seems more complicated than PHP where you can just mix them together. However, the way that PHP mixes these things is a really bad idea that leads to multiple security vulnerabilities).

0👍

Your static folder is not where it’s supposed to be. Try this:

mysite
    static
        js
        css
        images
            b-img-4.jpg  

If your image is placed as shown in the above tree, this should work:

<img src="{% static "images/b-img-4.jpg" %}" alt="My image"/>

Leave a comment