Home > Back-end >  My html isn't connecting to my css only when running trought server
My html isn't connecting to my css only when running trought server

Time:12-21

I am making a Django app and when I try to run the different pages the HTML is loading but not the CSS, when I open the files normally (not from the server) it all works fine, but when I run them through the server it searches for the CSS at the URL page. Here is an example: at URL:http://127.0.0.1:8000/profile/create/ the HTML is all working but CSS isn't and I get this in the terminal:

[21/Dec/2022 10:11:54] "GET /profile/create/ HTTP/1.1" 200 1374
Not Found: /profile/create/style.css
[21/Dec/2022 10:11:54] "GET /profile/create/style.css HTTP/1.1" 404 2993

The HTML and CSS are in the same directory and here is my connection from my HTML

    <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="style.css">
    <title>MyPlantApp</title>
</head>

CodePudding user response:

You need to create a folder for your static files and store all your static files there. set your static files directory using this code in your settings.py file

STATICFILES_DIRS = [
(os.path.join(BASE_DIR, "static/")),

]

your static folder should look like this static file folder Also you need to use the proper templating syntax to specify find your static files. change <link rel="stylesheet" type="text/css" href="style.css"> to <link rel="stylesheet" type="text/css" href="{% static 'style.css' %}">

CodePudding user response:

Have you tried putting / before style.css? You are trying to get style.css from relative path of HTML file. If u put / before style.css it will try to get css file from root directory.

/style.css instead of style.css

<!-- Your Code -->
<link rel="stylesheet" type="text/css" href="style.css">

<!-- Might help -->
<link rel="stylesheet" type="text/css" href="/style.css">
  • Related