Django Appending Slashes to Static File URLs on OpenShift - django

I am attempting to deploy a Django 1.6 application on OpenShift using the Python 3.3 cartridge, but I have run into problems with static files. I have had partial success with the OpenShift IRC channel, tutorials/templates (for example), and previous StackExchange questions (for example), but nothing has completely resolved the problem.
When I request the static content by URL (e.g. 'mydomain.com/static/stylesheet.css' or 'mydomain.com/static/icons/cog.svg') I can see them perfectly fine. When static files are used as SVG data for icons, they show up fine. Only when linking to a stylesheet have I run into problems. I use the following to include CSS in my template:
<link type="text/css" rel="stylesheet" href={% static "stylesheet.css" %}/>
I have loaded the static files tag set with {% load staticfiles %}. Instead of seeing the stylesheet at /static/stylesheet.css, Django (I assume that it is Django, not Apache) looks for it at /static/stylesheet.css/ (note the trailing slash). This causes the request to fail with a 404 status code. The same thing occurs when I use other file extensions (I have tried .txt, .css, and .svg) or link to a file contained in a subdirectory of static. It is only in this circumstance that an extra trailing slash is appended.
It is my understanding that Django appends a trailing slash to a URL in the event that the URL does not match any of the patterns defined in urls.py. Is it possible on OpenShift to configure Apache so that it directly handles all requests to URLs of the form /static/*? I have an .htaccess file in the wsgi directory with the commands
Rewrite Engine On
Rewrite Rule ^application/static/(.+)$ /static/$1 [L]
but this does not solve the problem. I have also tried using a rewrite rule for just the stylesheet as well as a few things with Alias but have had no luck there, either.
Should Django be getting the requests for these static files at all? I have confirmed that DEBUG is being set to False in my settings.py file, and make no mention of django.views.static.serve in my urls.py file. Here are the relevant parts of settings.py:
STATIC_URL = '/static/'
if 'OPENSHIFT_REPO_DIR' in os.environ:
STATIC_ROOT = os.path.join(os.environ.get('OPENSHIFT_REPO_DIR'),
'wsgi', 'static')
else:
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
I do not set values for STATICFILES_DIRS or STATICFILES_FINDERS because at present I'm only dealing with static files found at STATIC_ROOT. The OpenShift project looks like
~/app-root/runtime/repo/wsgi/
.htaccess
application
openshift/
settings.py
manage.py
#And so on.
static/
stylesheet.css
icons/
cog.svg
#More icons here.
This is my first time trying to deploy and I am stuck on this stumbling block. Does anyone know what I am doing wrong?

Instead of href={% static "stylesheet.css" %}, try href="{% static 'stylesheet.css' %}"

Related

Django starts putting a slash in front of my static url relative path in 3.1

I had this code that worked ok in 3.0.8
<script src="{% static 'js/main.js' %}"></script>
with the value in settings:
STATIC_URL = (
"static/" # /static/ for django web development, static/ for django-bakery build
)
when building with bakery it works only if I supply the relative path.
But since 3.1, as per their release notes: The STATIC_URL and MEDIA_URL settings set to relative paths are now prefixed by the server-provided value of SCRIPT_NAME (or / if not set). This change should not affect settings set to valid URLs or absolute paths. (https://docs.djangoproject.com/en/3.1/releases/3.1/#backwards-incompatible-3-1) it puts a / in front of my static url and the build fails.
Any ideas how to avoid it? I use django bakery to bake the site into a single file, no server.
Update: django-bakery will build without errors, but then the index file will have the script like this:
<script src="/static/js/main.js"></script>
which when you open the index.html, you will receive a 404, /static/js/main.js not found. while if you replace it with static/js/main.js it will work, since the static folder is in the same folder as the index.

Django can't find the static files

I just started a new project and currently Django can't find the static files. I'm using Django==2.2.6
The static files are located in an app called "website". This is the file structure.
https://i.imgur.com/AnPACop.png
This is from the settings:
STATIC_URL = '/static/'
This is how i include the static file:
{% static 'css/style.css' %}
The URL to the static file seems correct:
<link href="/static/css/style.css" rel="stylesheet">
EDIT: its NOT correct. But this works:
<link href="/static/core/css/style.css" rel="stylesheet">
Make your file structure like the following one:
ProjectFolderName
static
- css
- js
template
website
projectfoldername
migrations
Put your static folder in your project folder. Then make these changes to your settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
Then run this command:
python manage.py collectstatic
You static file will be copied to New file created by django as assets.
and add to your HTML
{% load static %}
This is the URL that the browser will find your static files. It won't let Django know in which folder to find them inside your project root (`BASE_DIR)
STATIC_URL = '/static/'
Try using this instead to specify the directory you are storing the statics
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'website/static'),)
Also, make sure you are loading the statics in your template with the following template tag
{% load static %}
Update
The path to the CSS is also wrong on the html you should change it to:
<link href="/static/core/css/style.css" rel="stylesheet">
It's solved. The problem was the file structure. For some reason the static files was in a core-folder.
https://i.imgur.com/AnPACop.png
When i put the files directly in "static" it started working.
My english is not perfect, sorry in advance.
I'm in Django 3 and I had the same problem. This how I find what's wrong, with the help of everyone up me. Just consider this post like a note for me.
I do :
python3 manage.py runserver
At this moment I read the last line of the output. It was looking in a file that did'nt exist. I copied the path. Go in terminal and :
cd path/copied/before/static/base.css
File not found. At this moment I know what to do. Just follow the path and create the folder I need.
I know it's not a good practise but it's can help beginner.

Django - cant find my static files even after manually correcting the path

I've encountered a problem that, even after an evening of trying , I've not yet been able to resolve.
For my project I have a single static directory, and I've included the css files for the 'login' page in there. Yet when I try to load the webpage I get a 404 error.
When I try to find the files with the 'findstatic' or 'collectstatic' commands the files get skiped. Its like the 'static' directory is invisible to django. I've even directly copies the 'Login' folder to 'static_root', but even then the program was Django was unable to find the files.
Does anyone have an idea what I am doing wrong?
My project folder looks like this:
* MPS (main folder)
* Scraper (app)
* Database (app)
* Templates
* Static_root
* Static
* Login
* css
* style.css
My settings.py has been configured like so:
STATIC_URL = 'static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root/')
And I call the static files in the template with the following code:
{% load staticfiles %}
(...)
<link rel="stylesheet" href="{% static 'login/css/style.css' %}">
Your project folder structure looks a bit different from most Django projects. Traditionally it would look something more like this:
myproject/
urls.py
wsgi.py
settings.py
myapp/
static/
myapp/
css/
style.css
js/
index.js
manage.py
Notice that the app static files of myapp is within a subfolder called the same as the app folder. This is to easily namespace the files as you can read more about in the docs.
Now we might be able to get away with putting our static files directly in my_app/static/ (rather than creating another my_app subdirectory), but it would actually be a bad idea. Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those static files inside another directory named for the application itself.

Serving static images on webfaction-hosted django site

I'm using a webfaction-hosted django site, and I'm trying to get locally stored images to show properly, but I'm having trouble getting the static directories/permissions/etc. to line up.
I've configured everything according to webfaction's guide at https://docs.webfaction.com/software/django/config.html
In my project's templates folder, I have the line:
<img src="{{ STATIC_URL }}{{ project.overview_image }}">
where project.overview_image is a CharField where I can list a subdirectory where the image is located.
In my settings.py, I have:
STATIC_URL = 'http://morphogen.cc/static/'
STATIC_ROOT = '/home/scottnla/webapps/static_media/'
STATICFILES_DIRS = (
'/home/scottnla/webapps/portfolio_website/portfolio_website/static/'
)
where /webapps/static_media/ is my static media app.
inside of my project's local /static/ folder, i have an image in the folder
/home/scottnla/webapps/portfolio_website/portfolio_website/static/images/small/img.jpg
and when i run 'manage.py collectstatic', the image is copied to:
/home/scottnla/webapps/static_media/images/small/img.jpg
but when I look at my served HTML page, the image doesn't show.
When I inspect the element, I see that the image source is listed as:
<img src="http://morphogen.cc/static/images/small/img.jpg">
which seems consistent with everything above, but if I go directly to that directory, I get a 403 Forbidden Error.
What's the next step in troubleshooting this?
thanks.
Your settings.py should look like this.
STATIC_ROOT = '/home/USER_NAME/webapps/static_media/'
STATIC_URL = '/static/'
STATIC_DIR is outdated and unnecessary for a single static folder
ALSO, you must have
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
IF you're still having problems check the Django Static Files Documentation

Pycharm + Django 1.3 + STATIC_URL in templates = Unresolved static reference

PyCharm (1.3 and 2 beta) in my Django 1.3 project throws a lot of "unresolved static reference" errors when inspecting my templates for script and style includes.
In an outdated PyCharm doc, I found that a small guide that doesn't work in my situation, because my static files are spread over multiple apps. Adding my static dirs to STATICFILES_DIRS also didn't work.
Dir structure (simplified):
app1/static/js/file.js
app1/static/css/file.css
app2/static/js/otherfile.js
app2/static/css/otherfile.css
templates/template.html
­
Template.html:
<script src="{{ STATIC_URL }}js/file.js"></script>
file.js resolves when I visit the template on localhost, but not in PyCharm.
How do I make static files resolve in PyCharm?
Go to Settings in Pycharm 2.73
Settings >> Project Setting >> Django
Enable the Django support and provide the paths for the three following files:
Project Root
Settings file
Manage.py file
When you have given these informations, close PyCharm and restart it.
PyCharm 2.5 finds my static files again.
The trick is to mark app1/static and app2/static as "Source Root".
STATICFILES_DIRS is not working for me.
The selected answer doesn't work for me. What solved it is using a prefix in STATICFILES_DIRS:
STATICFILES_DIRS = (
# ...
("resources", "C:/data/django/myproject/myapp/static"), )
as documented in the django docs: https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/
Then in your html template:
<link rel="stylesheet" href="{%static 'resources/favicon.png' %}" type="text/css">
STATICFILES_DIRS works for
{% static "js/lib/jquery-1.8.2.min.js" %}
tag in template.
Not for {{ STATIC_URL }}js/lib/jquery-1.8.2.min.js
http://youtrack.jetbrains.com/issue/PY-5568