Django app not updating static files in browser - python

The static files from third-party apps are not being updated in my Django site when it is served up in a browser with runserver.
Here is my file structure (with many more static files):
- mysite
- mysite
- settings.py
- wsgi.py
. . .
- myapp
- templates
- base.html
- myapp.html
- models.py
- forms.py
. . .
- static
- MyApp
- mystyle.css
- autocomplete-light
- select2.css
base.html:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>
{% block title %}Portal{% endblock title %}
</title>
{% block stylesheets %}
<link rel="stylesheet" type="text/css" href="{% static 'myapp/mystyle.css' %}?{% now "U" %}"/>
{% endblock stylesheets %}
</head>
<body>
{% block content %}
{% endblock content %}
</body>
<footer>
{% block footer %}
{% endblock footer %}
</footer>
</html>
myapp.html:
{% extends "myapp/base.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block title %}My App{% endblock title %}
{% block stylesheets %}
{{ block.super }}
{% endblock stylesheets %}
{% block content %}
{% crispy formset helper %}
{% endblock content %}
{% block footer %}
<script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}?{% now "U" %}"></script>
{{ formset.media }}
{% endblock %}
settings.py (relevant piece):
STATIC_ROOT = 'C:/Users/username/mysite/static/'
STATIC_URL = '/static/'
I used python manage.py collectstatic to collect the static files for my app and the third party app (django-autocomplete-light) into the STATIC_ROOT. They exist in the STATIC_ROOT. I then served up my site with runserver. The static files are loaded with the site and their paths match back to the STATIC_ROOT. When I edit the static files (i.e. select2.css for the autocomplete app), the changes show up in STATIC_ROOT. However, they do not show up in the browser.
I have tried clearing my cache, serving the site in various browsers, killing/restarting runserver, killing/restarting my text editor, and rerunning collectstatic. None of these attempts have worked - the static files simply will not update when the site is loaded.
How do I get the static files to update in the browser? I think I might be either missing a setting in settings.py, or it may be an issue with {{ formset.media }}. I am at a loss, though.

Remember add STATICFILES_DIRS, Django need the url to the staticfiles:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
'/var/www/static/', # If you have more staticfiles this will be the order
)
and also take a look to this What is the difference between {% load staticfiles %} and {% load static %}

Related

Static css file not loading in Django admin

I'm trying to load my own css file to replace base.css in order to customize Django Admin
{% extends "admin/base.html" %}
{% load static %}
{% block title %}VSPM{% endblock %}
{% block branding %}
<h1 id="site-name">{{ site_header|default:_('Django administration') }}</h1>
{% endblock %}
{% block stylesheet %}{% static "admin/css/theme.min.css" %}{% endblock %}
{% block nav-global %}{% endblock %}
But the css file isn't loading in Django admin. What am I doing wrong?
{% block extrastyle %}{% static "admin/css/theme.min.css" %}{% endblock %}
Change your block name and try
Go to your project.
Copy this folder /virualent_path//lib/python3.6/site-packages/django/contrib/admin/static/admin to 'static' folder of your project.
Add this string to the settings.py: STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
Then run this command: python3.6 manage.py collectstatic

Django Static image not displaying while using block

I am using Django, I am trying to display the image but I am getting the error.
Invalid block tag on line 35: 'static', expected 'endblock'. Did you
forget to register or load this tag?
If I added my image directly on index.html page then the image is displaying but when I am using extends and block to display then I am getting the error.
setting.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
)
home.html
{% extends 'demo1/index.html' %}
{% block content %}
<img src="{% static 'images/web/landing-page.png' %}" alt="Landing Page">
{% endblock %}
index.html
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title%}Home{% endblock %}</title>
<link rel="stylesheet" href="{% static 'css/style.css'%}" type="text/css">
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
You missed the open { in your home-page
{% extends 'demo1/index.html' %}
{% load static %}
{% block content %}
<img src="{% static 'images/web/landing-page.png' %}" alt="Landing Page">
{% endblock %}
NOTE
Django documentation prefers now {% load static %}.
{% load staticfiles %} works but I think it is deprecated.
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#static
Update:
From the Django docs:
The include tag should be considered as an implementation of "render
this subtemplate and include the HTML", not as "parse this subtemplate
and include its contents as if it were part of the parent". This means
that there is no shared state between included templates -- each
include is a completely independent rendering process.
Therefore please also load the static file in your home-page too
Do just {% load static %} on top of your home.html template.
You are missing a brace in your home.html
{% extends 'demo1/index.html' %}
{% block content %}
<img src="{% static 'images/web/landing-page.png' %}" alt="Landing Page">
% endblock %}
should be
{% extends 'demo1/index.html' %}
{% block content %}
<img src="{% static 'images/web/landing-page.png' %}" alt="Landing Page">
{% endblock %}

Why Django show me the path to the template but do not load the template himselfe into the page?

In the static folder of my DjangoServer is located a template of the default webpage. It's decorated with some template blocks.
If I load this template file, the path to the template is shown in the browser, it looks like, that the code is not loaded.
If I store the template in an app/template folder and I extend this file. It works very well. I use the tutorial of Django but it still not working.
Settings.py
django.contrib.staticfiles is added to INSTALLED_APPS
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
projectRootFolder/static/html/basePage.html
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}
<title>{% block title %}My amazing site{% endblock %}</title>
{% endblock %}
</head>
<body>
{% block body %}
<header>
{% block header %}
<header> -- HEADER BANNER --</header>
{% block menu %}<nav></nav>{% endblock %}
{% endblock %}
</header>
<section>
{% block section %} SECTION {% endblock %}
</section>
{% block footer %}
<footer> -- FOOTER --</footer>
{% endblock %}
{% endblock %}
</body>
<script type="text/javascript" src="{% static 'angularjs/SOME_ANGULAR_FILES_LOADED.js' %}"></script>
</html>
app/template/app/index.html from an app
{% load static from staticfiles %}
{% static "html/basePage.html" %}
{% block menu %}<nav>App A</nav>{% endblock %}
{% block section %} Lorem Ipsum{% endblock %}
app/views.py
from django.template import loader
from django.shortcuts import render
from django.http import HttpResponse
# PAGE CALLS
def index(request):
template = loader.get_template('mainControll/index.html')
context = {}
return HttpResponse(template.render(context, request))
Output
What is the mistake i've made? How can I load this template correctly?
Warning, this answer assumes your goal for asking the question is to get things to work, instead of helping you troubleshoot a probable permission problem just so that you can run into another problem. That is, I'm assuming your end goal is not a html document inside a non-html document.
What can you do to get your output to work:
It looks like you want to include a template into another template, you can do that with {% extends "basePage.html" %}. Your template does then need to be at a location where templates are found, not where static pages are found.
E.g. If your app is called 'myapp' then under myapp/templates/ is a one possible place, assuming the TEMPLATES setting has APPDIRS = True
This would mean changing index.html to be
{% extends "basePage.html" %}
{% block menu %}<nav>App A</nav>{% endblock %}
{% block section %} Lorem Ipsum{% endblock %}
See https://tutorial.djangogirls.org/en/template_extending/ for example of this and https://docs.djangoproject.com/en/1.11/topics/templates/#configuration for configuring things to that your basePage.html can be found

Unable to load template file for custom widget

I'm trying to create a custom widget(named Status) with django-dashing.
This custom widget is 99% copy-paste from libs own NumberWidget.
I have js, html and css files in status/widgets/status and initiated in dashing-config.js
in the browser console(Chrome) i get following message: "widget Status does not exist". So i have created my own {templates}dashing/dashing.html and loaded css/js files manually. Now i have placeholder for the widget, no error messages in console and i see successfull ajax requests, but the widget body is empty.
So widgets html file is not loaded, but why?
After reverse engineering of Dashing.utils.js i found out that example dashboard.html from the docs is incomplete and templates block is missing. So full dashboard.html should be like:
{% extends 'dashing/base.html' %}
{% load staticfiles %}
{% block stylesheets %}
<link rel="stylesheet" href="{% static 'widgets/status/status.css' %}">
{% endblock %}
{% block templates %}
<link rel="resource" type="text/html" href="{% static 'widgets/status/status.html' %}" data-widget="status">
{% endblock %}
{% block scripts %}
<script type="text/javascript" src="{% static 'widgets/status/status.js' %}"></script>
{% endblock %}
{% block config_file %}
<script type="text/javascript" src="{% static 'dashing-config.js' %}"></script>
{% endblock %}

Creating Modular templates with template inheritance

I am new to Django and I am practicing template inheritance. I am currently having trouble inheriting templates on the 3rd level. The base level is a template that my whole site uses (ex: navbars). The second level is the content of my site. However this content is a bit lengthy so I took a portion(contactform.html) of it and created its own HTML file for that portion.
I am able to get my home.html into my index.html like so
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head lang="en">
<link href="{% static "css/boothie.css" %}" rel="stylesheet" type="text/css">
<script src="{% static "js/boothie.js" %}"></script>
<script src="{% static "js/jquery.easing.1.3.js" %}"></script>
<title>Boothie</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
Within my home.html I want to include my contactform.html. This is what I have so far.
{% extends "index/index.html" %}
{% load staticfiles %}
{% block content %}
...
...
...stuff...
<!-- contact -->
{% block contactform %}{% endblock %}
{% endblock %}
My contactform.html:
{% extends "home/home.html" %}
{% load staticfiles %}
{% block contactform %}
<section id="contact">
<!-- HTML! -->
</section>
{% endblock %}
This is what is currently in my home/views.py:
from django.shortcuts import render
from django.views import generic
class HomeView(generic.TemplateView):
template_name = "home/home.html"
my home/urls.py:
from django.conf.urls import patterns, url
from home.views import HomeView
urlpatterns = patterns('',
url(r'^$', HomeView.as_view(), name="home"),
)
TEMPLATE_DIRS:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'home'),
)
here is a picture of my project structure:
Instead of inheriting, just include the contactform template in the home template. In home/home.html put:
<section id="contact">
{% include 'home/contactform.html' %}
</section>
You dont need to extend, instead include
Example, keep the contents of the contact_form.html with just the required html content (without the extends, and the block tag, etc..), and then include the html snippet. Now, django would do the magic for you - The included snippet would have all the context variables too.
{% extends ".." %}
{% load staticfiles %}
{% block content %}
...
...
...stuff...
{% include /path/to/contactform.html %}
{% endblock %}

Categories