简体   繁体   中英

Django issue with linking css to html

I am aware that there are other posts on this, however, none of the answers have helped fix my issue. I am making a django project and would like to use some basic css on my page, however it does not seem to be linking. The html file is in a folder called templates, while the css is in a folder called static. Here is what I used to link my css.

<link rel="stylesheet" type="text/css" src='../static/style.css'>

And my css file looks like this:

body {
  background: black;
}
h1 {
  color: white;
}

Any help would be appreciated.

The method you used is for only connecting HTML to CSS normally but in your case your code should look like this:

<link rel="stylesheet" type="text/css" src="{% static 'path/to/file/'  %}">

and don't forget to put {% load static %} at the top of your file. Note: the 'path/to/file' should start from within the static dir. Example: For a file structure like this:

|--static
|  |-- css
|      |-- styles.css

Your link should look like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM