Static files not loaded into templates - django

I am new to Django and so far all I know about static files is that they are CSS, JS, and images and they should be in a directory called static within the app directory
but when I use one of these files in my template like that:
first I load the static files
{% load static %} <!-- in the 1st line of the template -->
then I link the CSS file like that
<link href="{% static 'auctions/styles.css' %}" rel="stylesheet">
They don't load and the styles don't seem to appear
so I just want to know what I am missing here
this is the project tree enter image description here
static root and url from settings
STATIC_URL = '/static/'
STATIC_ROOT = 'E:/Work/SoftwareDevelopment/Web/Django/commerce/auctions/static'

Did you ran python manage.py collectstatic?
You also need to configurate your settings.py with STATIC_URL = '/static/'
Docs

Are your auctions's static folder separated in css, js, images folders? If so, you are missing specifying that in static:
'auctions/css/styles.css'
If that doesn't work, try running collectstatic manually and getting the path to the file from there to pass in the html.

Related

Django - Load static files from another app

In app1 I am trying to load static files from app2. I set no STATICFILES_FINDERS in project settings.py, which means, Django will use default AppDirectoriesFinder when it finds static subdirectory inside app directory.
Problem:
In template files of app1, I can generate urls of static files for app1 very easily. But if I want app1 template files to generate urls for static files of app2, links are not working. How can I in app1 generate static files of app2?
App1 template file:
{% load static %}
<img src="{% static "app1/example.jpg" %}"> <!-- ok -->
<img src="{% static "app2/example.jpg" %}"> <!-- link broken -->
HTML Output:
<img src="http://localhost:8000/static/app1/example.jpg">
<img src="http://localhost:8000/static/app2/example.jpg">
I had the same problem. I handled it setting this var in settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'app1/static/'),
os.path.join(BASE_DIR, 'app2/static/'),
]
So, both dirs become avaliable in static template tag regardless from which app you're calling it.
I'm using django 2.1.
Obs:
1 - Maybe this come set by default when you use the startapp command. Idk.
2 - BASE_DIR is the abs path to settings.py.
I found a solution. Please be aware, that directory in your project folder, which is named exactly same as your project folder is not an app. At first thought it is the initial app, that is automatically created by Django, but it isnt.
If you have two apps, and you want to load static files between them, code examples above works.
Just add this to your settings.py (from the Django documentation)
STATICFILES_DIRS = [
BASE_DIR / "static",
'var/www/static/',
]

Django not finding my static files

I'm trying to reorganize my project, as the files in it are very unorganized. There is a static folder in the main project folder that should only have all of the apps. So I'm relocating some of the .js files into their respective apps.
One of the files is in /static/js/mmm and I'm trying to move it to mmm/static/mmm. I copied the file over and changed the code in one of my templates (located in mmm/templates/mmm) from
<script src="/static/js/mmm/filemanage.js" type="text/javascript"></script>
to
{% load staticfiles %}
<script src="{% static "mmm/filemanage.js" %}" type="text/javascript"></script>
However I opened the page and the js console and it is trying to access the file like this:
http://fakedomain.com/static/mmm/filemanage.js
From my understanding it should be looking in
http://fakedomain.com/mmm/static/mmm/filemanage.js
In my settings file I have 'mmm' as an installed app and
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'assets'),
)
STATIC_URL = '/static/'
Not sure what I'm doing wrong here as I don't totally understand how Django searches for the static files. I also didn't put those things in the settings files so I don't understand what they're doing. The Django tutorial, part 6 says that "Django’s STATICFILES_FINDERS setting contains a list of finders that know how to discover static files from various sources. One of the defaults is AppDirectoriesFinder which looks for a “static” subdirectory in each of the INSTALLED_APPS, like the one in polls we just created. "
I believe your problem is with this line:
<script src="{% static "static/parcelManage.js" %}" type="text/javascript"></script>
Reveiw the documentation for static files here.
Basically, it looks like you should change the line to:
<script src="{% static "measuring/parcelManage.js" %}" type="text/javascript"></script>

Django template does not render CSS and Javascript

I have the following folder structure for my project.
-App1
-App2
-App3
-App4
-static
-css
-bootstrap.css
-js
-bootstrap.js
-jquery.js
-tempaltes
-base.html
Now, in my base.html file i have
But, when i view the file the css and javascript does not seem to be loaded on the page.
In my, i have
settings.py
TEMPLATE_DIRS = "Absolute-path-to-base.html"
STATIC_URL = '/static'
STATICFILES_DIRS = 'Absolute-path-to-the above static folder'
As per all the docs and posts what i understood was, we need to keep all the staticfiles in one place, viz, static folder in my case...and all the templates (including base.html) in one place. After doing so, i open the base.html in my browser to view the page...and it does not display the CSS and the javascript. Instead when i place the file (base.html) in the static folder things work fine.
Can someone point me in the right direction?
You should replace
STATIC_URL = '/static'
by :
STATIC_URL = '/static/'
and in django templates, you should use
<script src="{% static "path" %}"></script>
also add {% load staticfiles %} at the top

static files in pybb

I have some problems with static file, when i use them in pybb. I work on django 1.3 and python 2.6.5.
My settings file is here
I put my css and images in /static/pybb/css and /static/pybb/images
when i put tag like
<link rel="stylesheet" href="{{ STATIC_URL }}pybb/my.css">
in my templates, it doesn't work. This is the same with image
<img src="{{ STATIC_URL }}pybb/images/myimage.jpg">
To sum it up, my own static files don't appear in my project templates.
any idea? perhaps i forgot something
Why are you putting css in package static files directory?
Also directories in STATICFILES_DIRS should not overlaps. Look at example provided with pybbm or even start project from this example.

Django - Static files serving javascript , css and image files

I need to server static files in my Django project.
I would like to place them in /static directory and be able to reference them in my templates.
I've been reading "Managing static files" in the documentation and I am confused. I've followed the instructions but am not able to get it to work.
1) I have put my static files in /static under each app in my project.
2) django.contrib.staticfiles is included under my INSTALLED_APPS.
I've set the following variables in settings:
STATIC_ROOT = '/static/'
STATIC_URL = '/static/'
In my template I have the following line:
<script type="text/javascript" src={{ STATIC_URL }}/a_ajax.js></script>
however when I bring up the page and look at source the line is:
<script type="text/javascript" src=/a_ajax.js></script>
It seems like nothing was passed to the template.
What am I doing wrong?
Considering that you are using Django 1.4 or higher, here is my explanation:
In Django 1.3 the static files were separated from the uploaded files (media) and now, they have their own home.
To serve static files (css, js, images, etc) in the development environment you need:
Put these files in their respective app's static subdirectory
Make sure you have django.contrib.staticfiles.finders.AppDirectoriesFinder in your settings.py (this is the default behavior)
The directory structure will be:
__| project_root/
____| your_app/
______| static/
________| test.css
________| some.js
So the request to http://localhost:8000/static/test.css will be routed to your_app/static/test.css.
Note that you don't need to change STATIC_ROOT setting, until you go to production environment that you will need to run ./manage.py collectstatic.
The collectstatic command will collect files from the app's static subdirectories and put them all in an unique place (the directory defined in STATIC_ROOT)
If you are going to deploy your project in production, see ./manage.py collectstatic documentation.
Include 'django.core.context_processors.static' is in your TEMPLATE_CONTEXT_PROCESSORS var in settings.py.
see documentation: Referring to static files in templates
Update:
And in the view, use the RequestContext object instead of the Context object.
def view1(request):
...
context = RequestContext(...
return render_to_response( ... , context )
In newer versions of Django, this is how I link to static files in a html template:
<script type="text/javascript" src="{% static 'admin/js/labeler.js' %}"> </script>
Django docs on STATIC are here:
https://docs.djangoproject.com/en/1.9/howto/static-files/