0

I am working on a web-app using Django and everything is going well but there are two problems that I have been struggling with for the past week:

Problem 1: Within my project I have an app called User and it has the following structure:

User 
 >templates
  >User
   >Layout.html
   >Home.html
 >static
  >css
   >User
     >Layout.css
     >Home.css

I want to have the same header and left side menu across all my pages and here is how I went about it.

Layout.html

{% load static %}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="{% static 'User/css/Layout.css' %}">
{% block head %}
{% block title %}
{% endblock %}
{% block references %}
{% endblock %}
{% endblock %}
</head>

<body>
<header class="uppermenu">
</header>

<div class="leftsidemenu">
</div>

<div class="content"> 
{% block content %}
{% endblock %}
</div>

</body>
</html>

Home.html

{% extends "User/Layout.html" %}
{% load static %}

{% block head %}
{% block title %}
<title>Home</title>
{% endblock %}

{% block references %}
<link rel="stylesheet" href="{% static 'User/css/Home.css' %}">
{% endblock %}
{% endblock %}

{% block content %}
<!--Each page is going to have different content but the left side menu
and the header should remain the same -->
{% endblock %}

When extending Layout.html in Home.html, I want to add Home.css and not sure where to put it. Where should I insert my stylesheets in Layout.html and Home.html? Should they go in the "head", {% block references %}, {% block content %} or where? What is the best practice?

2) Problem 2 - (the annoying one)

I type "python manage.py runserver" and constantly reload pages when I make changes in my templates and static files to see how they look. I can see changes in Html files taking place immediately, whereas it takes about 1 hour (sometimes longer) for CSS changes to take place. It is so weird and annoying because it is slowing me down significantly.

For example: 
If I change <p1>Hello</p> to <p1>Bye</p> in Home.html and reload the
page, I could see the changes immediately and "Hello" changes to "Bye"                  
as it is supposed to be. 

But if I change p1 {color: red}; to p1 {color: green} in my Home.css 
then the changes take place about 1 hour later (didn't really set up a 
timer but it takes so long). 

I normally use Chrome but tried Safari and the result is the same. Could somebody help me out with these issues please? The second one concerns me more because I have no clue why it is happening.

import os


SECRET_KEY = 'whatever'


ALLOWED_HOSTS = []

INSTALLED_APPS = [
'Application',
'Home',
'Login',
'User',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

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 = 'Agent_WebApp.urls'

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    '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 = 'Agent_WebApp.wsgi.application'

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'AAA',
    'USER': 'root',
    'PASSWORD': 'AAA',
}
}



AUTH_PASSWORD_VALIDATORS = [
{
    'NAME':         

'django.contrib.auth.password_validation.UserAttribute     
SimilarityValidator',
},
{
    'NAME': 
'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
    'NAME': 
'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
    'NAME': 
'django.contrib.auth.password_validation.NumericPasswordValidator',
},

]


LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


STATIC_URL = '/static/'

EDIT: I am still in the development stage and my website did not go live yet

Sukhrab
  • 105
  • 2
  • 8
  • Have you tried deleting cache? This link might help. https://stackoverflow.com/questions/7013735/turn-off-caching-of-static-files-in-django-development-server – Julian Espinosa Aug 23 '17 at 20:09

3 Answers3

2

there might be in-browser caching of your CSS files this prevents Loading of the CSS file. to fix this you can choose one of the two solutions.

  1. You can simply do a " full" page refresh. by pressing ctrl and f5 or hold down the ctrl or shift button on your keyboard and click the refresh button (the circler arrow head on the left side of our browser)

  2. you can disable caching in the developer tool of your browser. if you are using chrome click the 3 dots on the top right side of your browser then click on more tools then click on Developer tools then go to the Network tab there you will see a "Disable Cache" checkbox on the left.

Note: watch the console where you are running runserver if you don't see a request for your CSS file that means it is using cached copies and one of the above approach should work but if you see request for your your CSS files then it is a different issue.

Edges
  • 21
  • 4
0

For my case when writing Django apps, usually reserve a block for adding CSS style sheets different from the general CSS that applies across all pages. I follow the following practices:

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Add all the css that will apply to all pages -->
    <link rel="stylesheet" type="text/css" href="{% static 'services/service/css/bootstrap.min.css' %}">
    <link rel="stylesheet" type="text/css" href="{% static 'font-awesome/css/font-awesome.min.css' %}">
    <link rel="stylesheet" type="text/css" href="{% static 'css/nav_fixed.css' %}">
    <link rel="stylesheet" href="{% static 'css/footer.css' %}">
    {% block addStyles %}
    <!-- Adding other custom styles for different pages -->
    {% endblock addStyles %}
    <link rel="shortcut icon" type="image/x-icon" href="{% static "images/logo.jpg" %}" />
    <title>{% block pageTitle %} Page title{% endblock pageTitle %}</title>
</head>

For issue 2: Could you give more details on how you have set up the STATIC_ROOT. Sometimes the problem could that you have to hard reload your browser by pressing CTRL + F5.

Regards

aspo
  • 374
  • 2
  • 9
  • Add the STATIC_ROOT and MEDIA ROOT to point to the location of the static folder and media folder respectively to the settings.py. Like this MEDIA_ROOT = os.path.join(BASE_DIR,'path_to_/media') STATIC_ROOT = os.path.join(BASE_DIR,'path_to/static'). Then run manage.py collectstatic command – aspo Aug 23 '17 at 20:40
  • See the following https://stackoverflow.com/questions/12204136/static-root-in-django-on-server – aspo Aug 23 '17 at 20:45
  • I should have mentioned it in my question: I am still in the development stage and my web-app did not go live yet. So, I am making changes on my local server and that is the reason I am confused as of why CSS files changes load so slowly, whereas HTML changes load immediately. – Sukhrab Aug 23 '17 at 22:31
  • While still in development, add the following to the settings.py, `# Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))`, and then the path for `STATIC_ROOT = os.path.join(BASE_DIR,'User/static')`. **This should make it easier for Django to find the static files**. For production, you will have to use tools like **nginx** to serve the static files – aspo Aug 24 '17 at 20:34
-1

Try to put the folder containing (settings.py,urls.py ,...) in the INSTALLED_APPS

Adarsh Wase
  • 1,727
  • 3
  • 12
  • 26
pouya
  • 81
  • 5