0

How can I add css to my django site?

Map structure:

  • portfolio-project
    • portfolio
      • static
        • css
          • style.css
    • templates

Now I have this in my html:

<!DOCTYPE html>
<html lang="en" dir="ltr">
 <head>
 <meta charset="utf-8">
    <title>Portfolio</title>
    {% load static %}
    <link href="{% static './css/style.css' %}" rel="stylesheet">
 </head>
 <body>
    <p>portfolio</p>
 </body>
</html>
6
  • Are you having issues importing the CSS? Or are you asking how to apply styles to the HTML? Please be explicit. Also, if you are having issues with static import please show us your static settings in your settings.py file Commented Dec 4, 2019 at 19:28
  • if i open it in my browser there is no styling added i only see portfolio as text Commented Dec 4, 2019 at 19:29
  • Do you expect it to be styling the <title>? If so, please read up how to apply CSS to HTML. It should have an id or class applied as such <title class="title-red">Portfolio</title> Commented Dec 4, 2019 at 19:30
  • this is my CSS: body { background-color: rgb(0, 0, 0); } Commented Dec 4, 2019 at 19:32
  • Does this answer your question? How do I use CSS in Django? Commented Dec 4, 2019 at 21:12

3 Answers 3

1

I think we're missing some info here, but you should use a relative path so it is looking in a directory relative to your project:

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

This will look for the stylesheet called 'main.css' in a folder using variables from your settings.py file.

Your STATIC_ROOT is where your static files will end up after running python manage.py collectstatic, STATICFILES_DIRS is the folder that collectstatic is going to get your static files from. STATIC_URL is the URL prefix that your static files will come from.

So if you have in settings.py:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

Your directory should look like this: /your_project_folder/static/css/main.css

And the template tag above will find your main.css file.

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

5 Comments

If you have that exact code I have to imagine it's your folder structure. Say you have your Django project in a folder called django. You should have your settings inside django/project/settings.py. Your css file should then be in django/static/css/style.css. Does your structure look like that?
Make sure the name of your CSS file matches across the board, in your site structure you are calling it main.css and in your sample HTML you have style.css.
What about your folder structure? What does that look like exactly?
i changed it exactly take a look.
Put the static folder on the same level as templates.
0

If you're in development, do thi's on your settings.py:

STATIC_URL = '/static/
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

Finally, in your html, this:

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

If you are in production, change STATICFILES_DIRS to STATIC_ROOT.

1 Comment

Can you send the repository on github?
0

You should replace the code below:

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

With the code below

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

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.