f1nn
f1nn

Reputation: 7057

How to attach CSS to my template?

I'd like to attach css to my template in Django. I created static dir in the main folder of my project and in settings.py added these lines:

import os
ROOT = os.path.dirname(os.path.abspath(__file__))
path = lambda *a: os.path.join(ROOT, *a)
PROJECT = os.path.basename(ROOT)

Then I defined vars in settings.py

MEDIA_ROOT = path('media')
MEDIA_URL = '/media/'
STATIC_ROOT = path('static')
STATIC_URL = '/static/'
STATICFILES_DIRS = path('static')

In template I link to css in a such way:

<link href="{{ STATIC_URL }}css/mycss.css" rel="stylesheet">

Django generates page with such source code:

<link href="/static/css/mycss.css" rel="stylesheet">

If I try to access this link (http://127.0.0.1:8000/static/css/myss.css) from Chrome source code viewer, it returns:

A server error occurred.  Please contact the administrator.

What am I doing wrong? Thanks in advance!

Upvotes: 1

Views: 1510

Answers (1)

Joseph Victor Zammit
Joseph Victor Zammit

Reputation: 15320

In development you have to add a specific URL pattern to your urls in urls.py.

The Django docs talk about this in detail here.

Note that you should disable this url in production. This answer to another, very similar question should help you fix this.

Upvotes: 3

Related Questions