Boritobison
Boritobison

Reputation: 105

CSS file not found. Django project

my django project wont find my css file. Spelling is correct, path is correct, lower and upper cases are correct, its linked in the head part. ive been searching for 2 hours now.

<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="../src/Templates/style.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
    -->
    <title>Document</title>

    <style>       
        .form-control{
          width: 50%;
        }
    </style>

</head>
<body>

  <div class="container" id="thisone">

    <h3 class="">BlaBlaBla!</h3>
    <h5>{{ message }}</h5>

    <form action="" method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>

  </div>

</body>

is this a stupid fail of mine?

Greetings

Upvotes: 5

Views: 15246

Answers (2)

Cenker Canbulut
Cenker Canbulut

Reputation: 41

According to the official statement "Generally, the templates folder is created and kept in the sample directory where manage.py lives. This templates folder contains all the templates you will create in different Django Apps."

As a Django projects grow in size it's often more convenient to have all the templates in one place rather than hunting for them within multiple apps.

Just as an additional information for the accepted answer's 1."Statement"

Besides, yes you should have your static folder at App-level and it should work!.

Upvotes: 1

Prabhat Singh
Prabhat Singh

Reputation: 1555

Check Your path again,
If it's correct Follow the Guidelines to Include CSS in Django Project

Static files are intended to wrap CSS files and your images, Django automatically identifies this file.

  1. Create static folder in your app folder, same directory as of migrations and template folder
  2. Create css Folder and insert it into static Folder
  3. Now put your styles.css into css folder
  4. Now in your HTML File where you want to include CSS, add {% load static %} On the top of HTML File and Your Path should be like this <link rel="stylesheet" href="{% static 'css/styles.css' %}"> in HTML file.
  5. Then Make Change To Your settings.py in projectfoldername with-

    STATIC_URL = '/static/'

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

    STATIC_ROOT = os.path.join(BASE_DIR, 'assets')

Then Run this command

python manage.py collectstatic

You static file will be copied to New file created by django as assets.

If it does not reflect changes Refer here If it does not work

Upvotes: 19

Related Questions