Search code examples
cssdjangodjango-templatesstatic-files

importing app css in Django


I not sure how to include css to my html. I have an app in my project that has it's own template folder. I want to get the sample.css into my html.

Something in the lines of <link rel="stylesheet" type="text/css" href="{% %}">

Not really sure what to put in between {% %}

structure:

/proj
   /app
      /templates
         sample.css
         sample.html

Here is my template_dirs:

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR,'templates'),
    os.path.join(BASE_DIR,'home/templates'),
    os.path.join(BASE_DIR,'gallery/templates'),
    os.path.join(BASE_DIR,'contact/templates'),
    os.path.join(BASE_DIR,'aboutme/templates'),
)

If any other info is required. Please let me know!

updated struture

/proj
   /app
      /static
         /css
            sample.css
      /templates
         sample.html

Solution

  • css files, as well as js files and images, are considered static files.

    The usual approach is to use django.contrib.staticfiles built-in django app to manage static files.

    If you set up the app correctly this is how your link would look like:

    <link rel="stylesheet" type="text/css" href="{% static 'sample.css' %}">