Static folder problems on multiple apps project on django - python

I'm new on Django and I have some problems with the static folder.
I created a "static folder" on the project root and inside I put folders for each app.
Django admin page is charging correctly
But CSS, JS, images and others on my index isn't.
How can I avoid have this problems in the future? For your answers, thank you so much.
settings.py
I set the {% load static %} in my html document
and i write the static tag like this example:
<link rel="stylesheet" href="{% static 'principal/css/bootstrap.min.css' %}" type="text/css">

Yo haven't created STATIC_DIRS where the django will find CSS,javascript files and the create the STATIC_ROOT where the django will store all the files
then run python manage.py collectstatic to let setup all the files

Related

Django: Couldn't import JavaScript files from static directory. (Jinja2)

I just started Django and I'm trying to import JavaScript Files from static directory. So basically I was working on a Flask project for some time, so to import javascript file I was always using - {{ url_for('.static', filename='js/js_file1.js' }}. Now I realized that I need to use other methods as - {% static 'main/js/js_file1.js' %}. So after some time, I was able to import my css files with - <link rel="stylesheet" href="{% static 'main/css/styles.css' %}">, they are in static/main/css/files..., although my js files are in static/main/js/files.... But for some reason, the JavaScript files won't import themselves. I'm importing them with - <link rel="application/javascript" href="{% static 'main/js/bootstrap.min.js' %}">.
Here are the network logs from Developer console, as I can see there are no JS files on their way to import themselves.
And here are the server Logs from the running server on cmd.
Developer Console Logs.
My settings.py file has this static_url, which I guess specifies the static folder.
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
My whole HTML File.
Firsteval, you have to do manage.py collectstatic, then on top of your template you have to import the static files with {% load static from staticfiles %}, finally you can import your file <script src="{% static 'js/myfile.js' %}"></script>

django css works on chrome but not firefox

I'm developing django website and am using bootstrap, I can see the styles on chrome browser but not firefox.
firefox inspection shows 404 error for the bootstrap and chrome doesn't. Thankful for any idea.
Code :-
#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__)))
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = '%s/coffestatic/' % (BASE_DIR)
STATICFILES_DIRS = ['%s/website-static-default/'% (BASE_DIR),
("bootstrap", '%s/bootstrap' % (BASE_DIR)),]
HTTP file.html
<head>
{% load static %}
<link href="{% static'bootstrap/css/bootstrap.css'%}"rel="stylesheet">
</head>
Files structure
BASEDIR
|--ProjectFolder
|------Apps
|--BootstrapDir
|------css
Work flow
Define static files
python manage.py collectstatic
Define styles in html
run project
Thanks
I'm not sure if your settings.py files are correct as they are different from how I would set things up. That being said, I'm no expert and you may well be running a different version of django to me. What I did notice though is you don't have a file type on your link file. Maybe Chrome is fine without this but maybe Firefox is having a hissy fit without it. Try changing:
<link href="{% static'bootstrap/css/bootstrap.css'%}"rel="stylesheet">
to
<link href="{% static'bootstrap/css/bootstrap.css'%}"rel="stylesheet" type="text/css">
Just a guess....
Using bootstrap starter file in your html file will help; as I have done the same and it helped.
Add these files at the end in body section:
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRS
and add these files at the starting in head section:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
I'm a beginner at django so not sure about it but it worked for me.
In project folder create a directory with any name. Let's name __shared__ and put the css and jss folders inside the directory
and in settings.py
at bottom put this code
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "__shared__"),
]

How to take a pre-designed webpage to django and add my own static files

I've designed a single page,including an 'index.html' and some css and 'js' files in separate folders. Then i decided to take my web page to django so that i can add database and a little management system to some contents in my web page. But I can't work it out. this is how I tried it so far.
url.py
urlpatterns = patterns('',
# Examples:
url(r'^$', 'mysite.views.home', name='home'),
)
views.py
def home(request):
return render_to_response('index.html')
setting.py
TEMPLATE_DIRS = ('/var/www/html/mysite/templates',)
I've put my index.html files and it's relatives folders into templates folder. But when i run the server it shows my homepage without recognizing my css and 'js' files.
I'm wondering what's the right way to do that.
Thanks for your help.
--Update 1--
So referring to this document I've set my static file stuffs like this
I've Created 'static' folder in my project ND Added one more line in 'setting.py'
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/'
Then I transferred all my 'css' and 'js' files to that folder.
I added this two lines to my home page to check if it works
{% load staticfiles %}
<img src="{% static "/img/b-img-4.jpg" %}" alt="My image"/>
Also I tried this command
python manage.py collectstatic
This is the error line for the new '' tag I added.
[24/Dec/2014 19:27:29] "GET /img/b-img-4.jpg HTTP/1.1" 404 2163
--Update 2--
I found out my mistake. I changed my <img> tag like this
<img src="{% static "admin/img/b-img-4.jpg" %}" alt="My image"/>
But problem is that it just recognizes its default static files and folders. I add my own file and folders and ran python manage.py collectstatic But It doesn't show the new files I add.
This is my folders srtructure
mysite--
|
mysite
| |
| static
| |
| my css js files
|
templates
|
index.html
What am I doing wrong?
You are nearly there, but you shouldn't be putting the other resources (JS, CSS, images) into the templates folder. That's only for templates! You need to read the static files docs:
https://docs.djangoproject.com/en/1.7/howto/static-files/
This is needed to ensure proper separation of your code (which includes templates - they are a kind of server-side code) and your resources.
(If you come from a PHP background, you might be frustrated by this, because it seems more complicated than PHP where you can just mix them together. However, the way that PHP mixes these things is a really bad idea that leads to multiple security vulnerabilities).
Your static folder is not where it's supposed to be. Try this:
mysite
static
js
css
images
b-img-4.jpg
If your image is placed as shown in the above tree, this should work:
<img src="{% static "images/b-img-4.jpg" %}" alt="My image"/>
So This blog made it all Crystal clear and with these settings I could make it works
setting.py
STATIC_URL = '/static/'
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, 'static_assets'),
)
index.html
{% load static from staticfiles %}
<img src="{% static 'img/author.jpg' %}" alt="My image"/>
Folder Structure
mysite--
|
mysite
| |
| static
| |
| my css js files
|
static_assets
|
templates
|
index.html

How to view images in django html template

I keep getting 404 error even though the url to the image is accurate. Yes, I have looked at official django docs and a whole lot of stackoverflow posts. I couldn't figure it out from those. If anyone can decipher what the instructions are saying to do then I will be grateful because they seem to be missing information.
index.html:
#I have tried static and staticfiles
{% load static %}
#a lot of code later...
<img border="0" src="{% static "logo.png" %}" alt="TMS" width="125" height="80">
Directory structure:
projectname
->__init__.py, settings.py, ...
manage.py
db.sql3
app_name
-> admin.py, models.py, views.py, templates->index.html, ...
static
->logo.png
The only thing about static that I have in settings.py along with 'django.contrib.staticfiles':
STATIC_URL = '/static/'
My views.py file has index function which returns HttpResponse(t.render(c)) where t is the index.html template and c is Context. Could those be the issue?
The url that results is this: http://127.0.0.1:8000/static/logo.png
You are using a static directory which is outside of your app (it is in the project folder), so in settings.py you need also:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)

django not found static files in the subfolder of static

django 1.6.5
My static files is under subfolder of static.
djangoprj
djangoprj
settings.py
urls.py
wsgi.py
__init__.py
static
myapp
mystaticfiles
...
apps
myapp
...
templates
*.html
...
In my templates file, I link static file as full path.
<img src="/static/myapp/images/my.png" />
But those file are not found. I got 404 error.
When change settings.py as
STATIC='/static/myapp/'
I can get those static files. But I am not want to do that. Why dose it not wok, when STATIC='/static/'? Is there some easy way to solve this problem instead of doing command manaully, such as 'collectstatic'? I have put my static file in static folder, and I have used full path in my templates file. Why does it not work? Thanks in advance.
Now that you switch the static file folder to '/static/myapp/'
You should use <img src="/static/images/my.png" /> in your tag.
Generally, you should use {% static %} template tag to do this, like below (assume STATIC='/static/').
{% load staticfiles %}
<img src="{% static 'myapp/images/my.png' %}" />
to learn more, see the tutorial, https://docs.djangoproject.com/en/1.6/intro/tutorial06/
I suggest you to read all the tutorial (part1 - part6) in https://docs.djangoproject.com/en/1.6/
So that you can know deep enough for the basis.
You can found some help in these answers also
static files problem
STATIC_URL Not Working
In my configuration I have a directory structure where static files are inside my app like this
djangoprj
djangoprj
settings.py
urls.py
wsgi.py
__init__.py
apps
myapp
...
templates
*.html
static
myapp
mystaticfiles
...
...
In settings
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
STATIC_URL = '/static/'
and in templates I use 'static' tags
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'posts/style.css' %}" />
I hope this can help you

Categories

Resources