Jaime
Jaime

Reputation: 323

How to use static content in django outside the static folder

I have a project with an app inside, and I want that app be able to be a reutilizable package, I have a template and in which I defined some CSS styles, but when I try to apply a body background image, I can't display it. I don't want to put this image in my global static folder, because this image is not for the project and it is only for this app. My project structure is the following: project-structure

login.html is my template and background.jpg is my image. My CSS code is:

body{
        background-image: url('templates/background.jpg');
    }

but this doesn't work.

Upvotes: 2

Views: 1430

Answers (2)

Shivam Mitra
Shivam Mitra

Reputation: 1052

You can create the static folder inside your app "app-package" and name of the folder should be "static". Every time the django sees a static file, it searches it in static folder in all your apps. Now you should load all the static files before writing a template.

login.html

{% load staticfiles %}
<html>
<head>
<style>
body  {
    background-image: url('static/background.jpg');
}
</style>
</head>
<body>
</body>
</html>

The image should be saved inside static folder and your settings for the project should know about this. Your settings for static files will look something like this. settings and static folder

Upvotes: 0

dani herrera
dani herrera

Reputation: 51645

1, where to store files

Quoting django docs (step 4):

Store your static files in a folder called static in your app

2, manifest

Then, read Packaging your app step 6 to know how to write manifest. Ex:

django-polls/MANIFEST.in

include LICENSE
include README.rst
recursive-include polls/static *

3, use static

Remember to use static on your templates:

{% load static from staticfiles %}
body{
        background-image: url( '{% static "templates/background.jpg%}');
    }

Upvotes: 1

Related Questions