Y
Y
Your_House2019-11-21 18:51:50
Django
Your_House, 2019-11-21 18:51:50

Django has stopped responding to changes in static files, and media files are loaded into a new media subfolder under the media folder. What's wrong??

I’m doing, doing, here’s a test site, I haven’t touched static files and media files for a long time. And so I needed to add styles to the connected (and seemingly working) css file. But doesn't work. And if, for example, you delete everything from this local css file (which is in the static folder of the application), write the "collectstatic" command, then NOTHING WILL CHANGE, all the previous styles will remain, although I erased everything from the css file.
Also (I don’t know if this problem is related to this one), when I try to upload media files (through the admin panel, as I did before), they are uploaded to the media folder in the media subfolder (which I myself create for some reason). Although before everything was normally loaded into "media", without creating any subfolder.
It seems that settings.py has not been touched for a long time. I don't know what happened.
settings.py

import os, sys

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(PROJECT_ROOT, 'apps'))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '!z3bsfob4ye-6$#q45xmfj48nrd$r*(nk203e!%$0_&nav)l1_'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

da
# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'News.apps.NewsConfig',
    'User.apps.UserConfig',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'Overwatch.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(PROJECT_ROOT, 'templates')
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'Overwatch.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'ru-RU'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/


# Static

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, "static")

STATICFILES_DIRS = [ 
  os.path.join(BASE_DIR, "Overwatch/static"), 
  os.path.join(BASE_DIR, "Overwatch/apps/News/static"),
  os.path.join(BASE_DIR, "Overwatch/apps/User/static"),
]

# Media
                                                     
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

urls.py (main)
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('News.urls')),
    path('user/', include('User.urls'))
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT

News.html (main page where the news is)
{% extends "main.html" %}
{% load staticfiles %}

{% block title %}News{% endblock title %}
{% block link_apps %}<link rel="stylesheet" href="{% static 'News/css/newsStyles.css' %}">{% endblock link_apps %}

{% block main_block %} 
    <div class="block-main center-block-main clearfix">
        {% for article in articles %}
        <div class="block-new">
            <a href="{% url 'News:article_details' article.id article.article_slug %}"><div class="new">
                <img src="{{ MEDIA_URL }}{{ article.article_img.url }}" alt="">
                <div class="text-new">
                    <div class="text-newh1">
                        <h1 class="texth1">{{ article.article_title }}</h1>
                    </div>
                    <div class="text-newp">
                        {{ article.article_text }}
                    </div>
                </div>
            </div></a>
        </div>
        {% endfor %}
    </div>

    <div id="navArticles">
        <ul>
            {% if articles.has_previous %}
                <li><a href="{% url 'News:mainS' articles.previous_page_number %}">&laquo;</a></li>
            {% else %}
                <li class="navArtUnv"><a href="">&laquo;</a></li>
            {% endif %}
            {% for page in articles.paginator.page_range %}
                {% if page == articles.number %}
                    <li><a href="{% url 'News:mainS' page %}" class="navArtSelect">{{ page }}</a></li>
                {% else %}
                    <li><a href="{% url 'News:mainS' page %}">{{ page }}</a></li>
                {% endif %}
            {% endfor %}
            {% if articles.has_next %}
                <li><a href="{% url 'News:mainS' articles.next_page_number %}">&raquo;</a></li>
            {% else %}
                <li class="navArtUnv"><a href="">&raquo;</a></li>
            {% endif %}
            
        </ul>
    </div>
    <p class="primerRaboti">Работает?</p>
{% endblock main_block %}

Project structure:
  • Overwatch
  • media
  • Overwatch
    • apps
      • news
        • static
          • news
            • css
            • img
        • admin.py
        • apps.py
        • ...
      • user
    • static
    • templates
      • news
      • user
      • main.html
    • settings.py
    • ...
  • static
  • db.sqlite3
  • manage.py

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question