added a new static file to my project by copy/paste into the static directory for my project. But I get 'File not found' when checking debug mode in the browser.
This is my folder structure:
- static
- projectname
-OldScript.js
-NewAwesomeScript.js
And this is my base.html
<script src="{% static 'projectname/OldScript.js' %}" type="text/javascript"></script>
<script src="{% static 'projectname/NewAwesomeScript.js' %}" type="text/javascript"></script>
This is the staticfiles_dir in settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
The oldscript.js have been in the project for a long time. Just wanted to add a new static file I need to load in base.html. So after copying the file to my folder I copy the include script code and changed the name to NewAwesomeScript.js.
It's not working and when checking debug in the browser, it says 'File not found 404'.
OldScript.j is loading, NewAwesomeScript.js is not loading.
Copy from Header for the old file that works:
Request URL:http://localhost:8000/static/projectname/OldScript.js
Request Method:GET
Status Code:200 OK (from disk cache)
Remote Address:127.0.0.1:8000
Referrer Policy:no-referrer-when-downgrade
And the Header for the new file:
Request URL:http://localhost:8000/static/projectname/NewAwesomeScript.js
Request Method:GET
Status Code:404 Not Found
Remote Address:127.0.0.1:8000
Referrer Policy:no-referrer-when-downgrade
Tried forcing refresh (ctrl + shift + F5)
I have restarted the server instance. It's in development. So not a production. Therefore I do not need to run 'collectstatic'. But desperate as I am, I also did this.
I have also checked read/write privileges.
Trying to find anything in the documentation about adding new static files. Without luck to finding my mistake.
What have I forgotten to do?
Related
I'm building a PWA in Django.
The serviceworker is located in
pwa->static->pwa->sw.js
Everthing gets loaded/cached and the serviceworker is running.
If in manifest.json "start_url": "/" or "start_url": "/pwa" is set, i get this serviceworker not found error, from the manifest, so it's not installable, but if I set it to "start_url": "." i can install my App but then I get:
Directory indexes are not allowed here.
Request Method: GET
Request URL: http://127.0.0.1:8000/static/pwa/
At app startup.
How can i overwrite or redirect this request to
http://127.0.0.1:8000/pwa/ ?
Django and service workers - serve "sw.js" at application's root url
The last entry solves my question.
Manifest, sw.js and index.html now in the template folder.
The assets in static.
Linking in sw.js
var filesToCache = ["/pwa", "/static/pwa/vue.js",...
I've tried different solutions already posted by users, but they didn't work for me.
settings.py of Project
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = False
ALLOWED_HOSTS = ["*"]
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
STATIC_ROOT=os.path.join(BASE_DIR,'assets')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
All my CSS files are in style folder inside the static folder.
And all images are in the media folder.
Browser Consol Logs
Refused to apply style from 'http://127.0.0.1:8000/static/styles/LandingPage_CSS.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
icons8-user-48.png:1
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Doorakart%20icon.png:1 Failed to load resource: the server responded with a status of 500 (Internal Server Error)
apple.jpg:1
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
banana.jpg:1
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
watermelon.jpg:1
.
.
.
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Refused to apply style from 'http://127.0.0.1:8000/static/styles/LandingPage_CSS.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
Example of HTML file
{% load static %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<title></title>
<link rel="stylesheet" href="{% static 'styles/LandingPage_CSS.css' %}">
</head>
...
# IMAGES ARE LOADED LIKE THIS
<img src="media/{{item.itemImage}}" alt="img" class=" card-img-top">
...
Also, I want to disable DEBUG because I want to make my custom 404 Error page.
404 page will also contain static Image and CSS, is it possible? Please help me with that too.
This is expected behavior. Django does not serve static files or media files in production. You should configure nginx, etc. to serve files.
As is specified in the Static file development view section of the documentation:
This view will only work if DEBUG is True.
That’s because this view is grossly inefficient and probably
insecure. This is only intended for local development, and should
never be used in production.
Normally you should configure nginx, apache web server to serve static files. These web servers are likely more efficient, and have more dedicated tooling for security.
Django offers some tooling to help you set up static files, for example with the collectstatic command [Django-doc] to collect static files in a single location. The documentation furthermore describes how to make a basic configuration for apache and nginx.
There is also a package whitenoise if you really want to let Django serve static files in production, but as said in the documentation:
Isn’t serving static files from Python horribly inefficient?
The short answer to this is that if you care about performance and
efficiency then you should be using WhiteNoise behind a CDN like
CloudFront. If you’re doing that then, because of the caching headers
WhiteNoise sends, the vast majority of static requests will be served
directly by the CDN without touching your application, so it really
doesn’t make much difference how efficient WhiteNoise is.
That said, WhiteNoise is pretty efficient. Because it only has to
serve a fixed set of files it does all the work of finding files and
determining the correct headers upfront on initialization. Requests
can then be served with little more than a dictionary lookup to find
the appropriate response. Also, when used with gunicorn (and most
other WSGI servers) the actual business of pushing the file down the
network interface is handled by the kernel’s very efficient sendfile
syscall, not by Python.
I had the same issue and you should update your nginx or any another serve configuration. Just add your media and static path like below.
location /media/ {
root /path/to/your/project;
}
location /static/ {
root /path/to/your/project;
}
Project path means where your media and static directories are located. Wish this helps you.
I have a website that works fine on my 'production server' with the url being something akin to:
https://mymain.site.com
I have had my sys admins build a site out for development and that url is:
https://mymain.site.com/development
It is a different IP address, but I believe the URL is wrecking havoc on django routing. Mostly I notice it in the static section of it.
If I goto the development server settings and change the static url to:
/development/static/
instead of /static/ like it was on production
None of my static files are found. Since the development server is a VM copy the root on the server for static files is the same. So if I run with the /development/static as the dev url it fails to load resources. If I run on my development server with the url being /static/ I am 90% sure it is grabbing the static files from the production server (at a different ip/url). Not totally sure what the fix is here? I am loking for any kind of ideas.
I suspect if the url of my development server was something more akin to:
https://mymain.site.for.development/
then this would work right out of the box from the code copied from the VM just repointing a few things.
So what am I missing to get this to work with the right static files?
I use a CDN for my static file hosting and this is how I have my settings.py file setup:
STATIC_HOST = 'https://cdn.host.com' if not DEBUG else ''
STATIC_URL = STATIC_HOST + '/static/'
When I'm developing I'll set DEBUG = True and everything works as expected. I would imagine you would have to do something like this:
STATIC_HOST = 'https://mymain.site.com' if not DEBUG else 'https://mymain.site.com/development'
STATIC_URL = STATIC_HOST + '/static/'
See if something like that would work.
I create a deployment of Django application with nginx and uwsgi. Where I try access to page rendered in pdf with wkhtmltopdf print the following error:
Command '['wkhtmltopdf', '--encoding', u'utf8', '--quiet', '/tmp/wkhtmltopdfQuoAXk.html', '-']' returned non-zero exit status 1
I think that wkhtmltopdf can't create the pdf, because the uwsgi request is not parse with HTML directly.
Do you think?
I solve this problem with help of this comment: https://github.com/incuna/django-wkhtmltopdf/issues/67#issuecomment-120930906
I changed in Django the STATIC_URL by default to:
STATIC_URL = 'http://domain.com/static/'
And I use "static" in templates as recommended the official documentation https://docs.djangoproject.com/en/1.8/howto/static-files/:
{% load staticfiles %} and
{% static 'js/jquery.min.js'%}"
Is there any way to get pyramid absolute application url in main() function?
I want to add it into global settings, so it could be called every where(in templates and js files).
In pyramid documents there is some functions would help, but all of them need a request object and must call in a view.
Thanks.
Pyramid (like most WSGI applications) can be mounted on any domain and url prefix. Thus the application itself doesn't actually know what urls it is responsible for unless you code that into your application specifically (an INI setting, for example).
This is why request.application_url exists... because the application_url could be different per request based on how many different domains and url prefixes you have that are proxying requests to your application.
I just get the full route for my index route, 'home' in my case:
I set this in my main wrapper mako template so that all of my JS calls can reference it to build a proper path for ajax calls/etc
<script type="text/javascript" charset="utf-8">
<%
app_url = request.route_url('home').rstrip('/')
%>
APP_URL = '${app_url}';
</script>