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'%}"
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.
When I'm using publicPath: '/static/' in my webpack config, my Vue.js app runs fine on a Django Webserver (both dev and production).
However now I'm trying to use history mode. I have to change the publicPath to "/", otherwise the URL always gets a "/static/" in between the domain and actual target.
The Vue.js dev server still runs fine, however both production and development Django server give me these errors in the browser console:
Uncaught SyntaxError: Unexpected token < Resource interpreted as
Stylesheet but transferred with MIME type text/html:
"http://127.0.0.1:8000/6.01a214ce.css".
I've tried several different solutions like:
publicPath: './'
assetsPublicPath: '/static/'
inside base html (gave me an error on compilation)
How can I resolve this issue?
it was actually a framework issue.. im using Quasar..
For some reason you have to change
base: process.env.VUE_ROUTER_BASE,
to
base: "/",
in router/index.js as the default seems to take the static url when you are using Django..
maybe it helps somebody
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?
I just put my Django application online using uwsgi. I can access it from any computer it working well.. the only thing is uwsgi can't load the css file stored in the /static/myapp/style.css path and I don't why.
The message I get when accessing a page on my site in the uwsgi console is :
GET /static/myapp/style.css => generated 98 bytes in 1msecs (HTTP/1.1 404) 3 header in 100bytes
But the file is actually in /static/myapp/style.css I can see it and it was working well in developpement, but now the website is in production it's not working anymore.
I added these to my settings.py but nothing changed :
STATIC_URL = '/static/' #Was enough in developpement
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = ("/home/user/Django/mysite/static"),
I also tried to add option in my uwsgi command like
--check-static /home/user/Django/mysite/static
Still not working...
I'm quite new to putting online a Django app so maybe it's not supposed to work like that.
My css file is on the same machine as my Django project.
I'm using django 1.11 and uwsgi 2.5.15.
I also tried to used nginx but I couldn't get it work properly and as I just want to access my app online I concluded that uwsgi alone is enough ( Am I wrong ? )
So if you have any idea that would be great !
Best regards
Gozu09