0

Silly question but i can't figure it out and it's too much. In a django project, in the same directory i have base_template.html and base_style.css. In a child folder i have search.html.

base_template.html
base_style.css
folder---- 
          search.html

Search.html extends base_template.html like so

{% extends "base_template.html" %}

Inside base_template.html i import base_style.css

<!DOCTYPE html>
<html>
<head>
    <title>{% block title %} Base template {% endblock %}</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="base_style.css">
    <link rel="stylesheet" type="text/css" href="awesome_font.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
    <style>
    {% block Additional_Styles %}
    {% endblock %}
    </style>

And there's no way it works. **Already tried:

./base_style.css
 /base_style.css
 base_style.css"/**

If i replace

<link rel="stylesheet" type="text/css" href="base_style.css">

with:

<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

It works, the css loads. I must mention that i just downloaded the css from the link and made no modifications.

Why doesnt the import work?

EDIT

settings.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],

 ....
STATIC_URL = '/static/'
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')

So the base_template is in MainDjangoApp\templates

2
  • Please try to avoid the underscore and use the midscore ! Commented May 23, 2017 at 12:19
  • Maybe you have to find out what the real docroot path is and then navigate to your template file. Commented May 23, 2017 at 12:20

2 Answers 2

2

You should pu your static files (.css, .js) in a static folder inside your app:

app
   static
       app 
          css
              base_style.css

adn in your template you should use

{% load static %}

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

You should definitely read Writing your first Django app, part 6 and Managing static files (e.g. images, JavaScript, CSS) on the djangoproject site.

Sign up to request clarification or add additional context in comments.

1 Comment

it still doesnt work. Can it be something from settings.py?
0

A solution would have been to change the folder of base_style.css as in the docs, however i added this to settings.py and i got it working. My app is not that complex so i won't get tangled in files.

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "templates"),
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.