0

Кастомные стили из blog.css не применяются, хотя прописаны после стилей бутстрапа (т.е. должны) В pycharm данный файл выделен красным. Прошу дать понять, где совершён фэйл(

settings.py

STATIC_URL = '/static/'

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

views.py:

from django.shortcuts import render
from django.utils import timezone
from .models import Post

def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'blog/post_list.html', {'posts':posts})

post_list.html

    {% load static %}

<html>
    <head>
        <title>Chris blog</title>

        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
        <link rel="stylesheet" type="text/css" href="{% static 'blog/css/blog.css' %}"/>
    </head>
    <body>
        <div>
            <h1 style="text-align: center"><a href="/">Django Girls Blog</a></h1>
        </div>

        {% for post in posts %}
            <div>
                <p>Published: {{ post.published_date }}</p>
                <h1><a href="">{{ post.title }}</a></h1>
                <p>{{ post.text | linebreaksbr }}</p>
            </div>
        {% endfor %}

    </body>
</html>

blog.css

h1 a {
    color: #FCA205;
}
body {
    background-color: brown;
}
p {
    color: red;
}
* {
    padding-left: 20px;
}

2 ответа 2

0

{% load staticfiles %} в post_list.html и прописать маршруты в urls.py

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
#......
#......
urlpatterns += staticfiles_urlpatterns()
0

Мало ли кому-то поможет - видимо статические файлы (в моём конкретном случае CSS-ник) надо кидать в папку 'static'. Т.е. В папке с самим сайтом (где у нас находится 'manage.py') создаём папку 'static', и туда сбрасываем все файлы. Для удобства думаю стоит там создать папку с названием приложения, а в ней уже сделать CSS-ник с <название_самого_приложения>.css

P.S. надеюсь мой немного болезненный опыт поможет кому-то меньше потерять времени =)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.