I am new in Django, previously I had a test site in which my static files were placed in static folders. There was a static folder for every app. But now I want to make a site, which will be deployed to python anywhere and so I have to have only one static folder.
This is the project folder: https://github.com/martin-varbanov96/fmi-fall-2016/tree/master/django/click_bait/miranda
You can see that in the settings file I have:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
in my target html file I have
<link rel="stylesheet" href="{% static 'home/css/style.css' %}" type="text/css">
which returns 404.
What is wrong how should I arrage my folders so that they are best working for the pythonanywhere host?
If you're getting a 404 error you should ensure debugging mode is on. If I assume it is on (as it is in the settings.py you have on GitHub), then your web server just isn't serving the static files. I would ensure you've run python manage.py collectstatic which will push the static files for the project to the static folder you've defined in settings.py.
I would also ensure that you've configured Python Anywhere to serve these files. According to their website (source: https://help.pythonanywhere.com/pages/DjangoStaticFiles/) you should ensure you have that static folder added so their server knows to serve the files. The steps at that link are as follows:
Go to the Web tab on the PythonAnywhere dashboard
Go to the Static Files section
Enter the same URL as STATIC_URL in the url section (typically, /static/)
Enter the path from STATIC_ROOT into the path section (the full path, including /home/username/etc)
Related
I was working with Django latest version by watching a tutorial on YT, but for some reason, my admin page isn't coming how it has to. It does not have a style or CSS.
[enter image description here][1]
[1]: https://i.stack.imgur.com/MrASY.pngenter code here
Are you running in debug or production mode?
Django delivers static files like CSS, JS etc in debug though it's own development server.
When you run in production mode / through a web server you have to configure your web server to deliver the static files.
Cheers
First of all I think you should first try this in the command line:
python manage.py collectstatic
After that go to settings file and check STATIC_URL and STATIC_ROOT
It should look like this(if you didn't change anything):
STATIC_URL = '/static/'
STATIC_ROOT = "/var/www/example.com/static/"
Static Url: URL to use when referring to static files located in STATIC_ROOT.
Static Root:
The absolute path to the directory where collectstatic will collect static files for deployment.
and also you can check the documentation.
I am trying to load static files in my django project. but it isn't loading. My static folder is in the base directory of project . Here I am giving some relevant code.
Here is my project's settings.py file's static part.
STATIC_URL = '/static/'
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assests')
Here is the html part
{% load static %}
<img id="undraw_shared_workspace_hwky" src="{% static 'undraw_shared_workspace_hwky.png' %}">
My project name is 'webpage' and here is the image path.
webpage\static\undraw_shared_workspace_hwky.png
I have tried...
manage.py collectstatic
to collect the static files. It worked and gave me a assests directory inside or root directory.
But when I go to the website it doesn't load the image. and the console says "Failed to load resource: the server responded with a status of 404 (Not Found)" .
I am in a windows machine and using Django 3.0.
Now I need you help to fix this problem.
Thanks in advance.
I am trying to run my static files from the same serving site in production mode. This is what I did.
In setting.py, I have set,
DEBUG = False
ALLOWED_HOSTS = ['12.10.100.11', 'localhost']
I included, 'django.contrib.staticfiles' in INSTALLED_APPS
I set the Static root directory to,
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
I ran,
python manage.py collectstatic
to copy all the static files to a local serving directory.
(following: https://djangobook.com/serving-files-production/)
I then point static url directory to,
STATIC_URL = ("/static/")
I then run,
python manage.py runserver 12.10.100.11:8000
However, on console inspect the error shows:
**Get http://12.10.100.11:8000/static/css/main.css 404 (not found)**
on my base.html looks something like this:
{% load static %}
<link rel="stylesheet" href="{% static '/css/main.css' %}" type="text/css" />
I am new to Django and need lots of advice, thanks
Jef
1)Pip install whitenoise
2)Include the following lines in wsgi.py
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
You can't serve static files. django with DEBUG mode turned off isn't responsible for that. Ngnix is better option with chache mechanisms for that.
You should check out Heroku platform how they deploy production ready django projects with whitenoise for example. Github has a lot of examples of opensource projects powered with django that you can learn from
On my local PC I can do "python manage.py runserver" and the site runs perfectly, CSS and all. I just deployed the site to a public server and while most things work, CSS (and the images) are not loading into the templates.
I found some other questions with a similar issue, but my code did not appear to suffer from any of the same problems.
Within the Django project settings the same python function is being used to allow the app to see the templates and the static CSS / image files. The templates are being found by the views and are loading without issue.
Both from settings.py:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates/css').replace('\\','/'),
os.path.join(os.path.dirname(__file__), 'content').replace('\\','/'),
)
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
)
In the base.html file which the rest of the templates all extend:
<head>
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static "style.css" %}" media="screen">
</head>
Directory structure:
|project_root/
|--manage.py
|--project/
| |--settings.py
| |--__init__.py
| |--content/
| | |--header.jpg
| |--templates/
| | |--base.html
| | |--css/
| | | |--style.css
My first thought when the CSS didn't load is that Django couldn't find the style.css file, but since I am using the same "os.path.dirname(file)" technique as with the templates, I am not sure this is the case.
What do I have wrong here?
Edit:
I neglected to mention that both the PC and server are running Python 2.7.5 and Django 1.5.5.
You never mentioned it in your post, so I am guessing:
You never ran ./manage.py collectstatic
collectstatic finds all static files (css, images, js) and puts them in a directory (Choose this directory with the django setting STATIC_ROOT) Then you point your webserver to that directory
You should deploy the static files using your server and not django.
The official documentation mentiones using collectstatic but unless your static files are messed up it's usually not a requirement. You just need to have some directory containing all your static files. Then you just push it with the server to the same place django will be looking.
Say you're STATIC_URL is '/static/', so you need to add an alias which would map '/static/' to the static directory. For example, using Apache, you should add this line to your http.conf:
Alias /static/ /path/to/mysite.com/static/
That's it! This thing goes true to media files as well, and it'd be wise to remove any serving of static files done by django for the development server (these kinds of urls). Finally, check out the documentation for even more information regarding other types of deployment
I am learning django and I already have a bit noobish question. I can not point to my static folder and I tried all the combinations, watched people do it in youtube tutorials etc.
My settings.py looks something like this:
STATIC_ROOT = '/home/peter/brewery/static/'
TEMPLATE_DIRS = '/home/peter/brewery/mysite/templates/'
where brewery/ is the folder containing mysite/ and static/, mysite/ is the folder created by
django-admin.py startproject
where settings.py also lives...
It seems that templates folder is mapped correctly, since the page renders with proper templates, it just cannot access the css in the /static/css/ folder. I show the path in my template for css like this
<link ... href='/static/css/brewery.css' />
I have also tried to make href absolute path on my computer and it does not work.
I am using django 1.3 and am running the server provided by django (python manage.py runserver)
1 - In your settings file, define a static url and static root like this:
STATIC_URL = '/static/'
2 - Set DEBUG = True
3 - Make sure your TEMPLATE_CONTEXT_PROCESSORS variable includes django.core.context_processors.static.
4 - Reference it in your templates like...
<link ... href='{{ STATIC_URL }}css/brewery.css' />
Source: https://docs.djangoproject.com/en/dev/howto/static-files/
In debug mode STATIC_ROOT is not used, but staticfiles_urlpatterns() provides static files from all different apps. Put your static files either into static/ directory in one of your apps or define STATICFILES_DIRS in your settings and put static files for the site there.
STATIC_ROOT is just the location where all static files are collected from all apps and STATICFILES_DIRS when you call:
python manage.py collectstatic
And it is only used in production environment.