I successfully integrated django in IIS 7.5 (server loads django1.6.5 using python2.7) but it doesn't load static files(CSS, JS), when I run manage.py runserver static files were loaded though. Am I missing something. I followed this tutorial in YouTube https://www.youtube.com/watch?v=kXbfHtAvubc. Thanks!
That video works great on my static file serving. Here is my environment: Python 2.7 Django 1.7 on Windows Server IIS 7.5
If you stick on steps, there shouldn't be any problem. Make sure you remove "Django Handler" in staticapp which is # 17:51 on that video. And check whether there is a static file handler in handler mappings. After you check your steps and make sure everything is right, you may try another way below to serve static file.
You may add a virtual directory to serve static file.
[How to fetch static CSS files with django on IIS?
Related
A Django-based service that I'm working on allows users to upload media files via REST API or Django admin but does not provide or use any static files (like css styles, js libraries, etc.).
Media files are stored in specific fields in database and use S3 bucket as storage backend so server itself does not directly serve any files at all.
Having such a case is running collectstatic command required every time application is being deployed?
Thought the concept of static and media files in Django application is rather simple I'm still confused about whether configurations for them should be somehow related?
As Django won't serve the staticfiles when the DEBUG is False (through deployment), if you want the css, js and other static files (at least in the admin, in your case), you need to run collectstatic.
So if you want django admin to be like your development environment (with css and images), you need it.
Your Django-based service does not need collectstatic at all.
Question is about django admin - because admin app uses static content css/img/js.
Do you use django admin in production?
If yes - then you need to run collectstatic command it, otherwise admin site will be unstyled or totally broken because of missing js files.
If no - then you collectstatic command has no use for you.
I'm getting an error when trying to serve static file on IIS using FastCGI with Django==2.2.5 and python==3.7.4
my settings.py
static code
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
I'm using wfastcgi version 3.0.0 and my server is windows server 2012 datacenter R2.
I tried all solution here Django Admin Page missing CSS in IIS and here How to fetch static CSS files with django on IIS? and watched some tutorial on youtube but still stuck with that.
The error when it shows when I'm trying to access the admin page
GET http://xxxxxx/static/admin/css/base.css net::ERR_ABORTED 404 (Not
Found)
It's difficult to figure what your environment situation may be, but you may me missing a Virtual link to your static directory in IIS. Also, you will need a web.config in your static directory. Your project will require a total of two web.config files.
Here is an example of what your static directory should look like https://github.com/Johnnyboycurtis/webproject/tree/master/static
You can review that project and follow along in the tutorial Deploy Django on Windows using Microsoft IIS
I am using DJango 1.8 and python 3.4.3, and I have been running my app on Debug mode, and found a way to show images inside a directory configured on MEDIA_ROOT, this was my first question and the solution I have found: How to upload and show images in DJango. But reading the docs I found that that solution is not suitable for a served app, so, if I stop using "Debug=True" the images will not be displayed, and I have to use one of the options exposed on this link: Static files on deployment but I don't have money to pay another server, I just can pay my hosting on pythonanywhere, and for the option to use the same server for the images, I don't have idea how to automate the collectstatic and also don't know how to trigger it when an user uploads a new image.
I have used ASP.NET and PHP5 and I didn't had problems with the images in none of them, so, I have two questions:
Is there an easy way to show images URL's?
Is there a high risk security problem if I deploy my app with DEBUG=True?
I hope you can help me, because I find this ridiculous, probably is for better security, but it just not make sense for a framework, instead of making the work easier it make it more difficult and stressful.
Django runserver is not intended for serving up static files in a production environment. It should be limited to development and testing environments.
If you are intending to use django's runserver to server up static files with DEBUG=False then use the --insecure flag.
You should never deploy a site with DEBUG = True due to security implications.
Static files and media assets are 2 different things.
Static Files
Static files are things like images you created and files that come with 3rd party apps you have installed (e.g. django-cms). These files include images, css and javascript files etc.). So you need to have a settings.STATIC_ROOT for this.
python manage.py collectstatic collects static files from different locations and puts them all in a single folder.
Media Files
Media files are things the user uploads (e.g. photos, documents etc.). So you have a settings.MEDIA_ROOT for this. collecstatic won't do anything to media files, they will just be there already once the user uploads them.
Serving up static and media files in production
Frameworks like Django aren't going to cover automatic production server configuration - that is something else you will have to learn unfortunately.
There are a lot of good guides around e.g. this one to help you get started serving media and static files in production.
Regarding server costs, I'm sure you can find a host to give you some free credit, or pay $5/month for a server somewhere... try lowendbox
Here is a guide from pythonanywhere regarding media and static files: https://help.pythonanywhere.com/pages/DjangoStaticFiles/
1) in urls.py add:
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
and open url http://myhost.com/media/
2) Never deploy a site into production with DEBUG turned on, DEBUG=True is a security issue,
I am developing a Django application and I want to put it online on DigitalOcean so I can show it to a remote future user. I am quite new to django and python and I have never in my life touched the deployment of web applications, so I'm a bit lost in all that.
I used the one-click install of a django droplet in digitalocean, which uses nginx and gunicorn. I was able to make the app load in the browser using the ip address (159.203.58.210), but the problem is that no static files can be loaded.
I googled that for some days and nothing could help me. I'm guessing it has something to do with either permissions not allowed or that I did not write the static files correcly in my templates and *.py files.
I'm running my application with the command:
python manage.py runserver localhost:9000
I'm using sqlite3 as db
My complete code is here:
https://github.com/gbastien1/carte-interactive
On the server, my app is at /home/django/international
(where international is my site name, the app is called carte_interactive)
My staticfiles settings in settings.py:
STATIC_URL = '/static/'
STATIC_ROOT= '/home/django/international/carte_interactive/static'
my staticfiles settings in /etc/nginx/sites-enabled/django.conf (same for media)
location /static {
alias /home/django/international/carte_interactive/static;
}
In my templates, I use {% load staticfiles %}
And I call my static files like so:
{% static 'carte_interactive/css/style.css' %}
And in my .py files, I sometimes call those files like this, which I don't know if it's right:
app_name='carte_interactive'
json_data_url = static('carte_interactive/json/data.json')
json_data_file = open(app_name + json_data_url, 'w') <-- here
It was the only solution that worked on development to access my static files in views.py, but it might be a problem in production with collectstatic and all, I don't know.
I am using django 1.9.4 on server and 1.9.1 on development it seems.
Do you have any idea why my staticfiles are 404 not found on the server but everything works fine on development?
Could there be a permission issue with the folder /home/django/... for the browser to reach my static files?
Is there a better way to call my staticfiles in views.py?
Thanks in advance!
edit:
The console error I get for all my template static files:
http://159.203.58.210/static/carte_interactive/css/style.css Failed to
load resource: the server responded with a status of 404 (Not Found)
I assume that you don't use nginx to serve static assets in
development? Runserver can serve static files, but very much slower
than nginx, which becomes a problem once you have more than a single
web site visitor at a time. You can remove the nginx alias for static
and reload nginx to let runserver serve the files, to confirm whether
it's a problem in the nginx config.
HÃ¥ken Lid
I removed nginx and made the Django server load the static files, and now I can show it to my future users. This answered my question, though it did not solve the problem itself! Thanks anyway !
I am running this python pyramid server. Strangely, when I moved my server code to a different machine, pserve stopped serving flash videos in my static folder. Whereas it serves other static files, like images, fine ! What could be a reason for this ?
Pyramid's static views serve up files with a content-type determined via python's mimetypes module. Most likely you'll need to add support for the extra video types to your installation at startup. Specifically something along the lines of the following, somewhere in your code.
import mimetypes
mimetypes.init()
mimetypes.add_type(...)
I possibly ran into a similar problem on my pyramid app. I'm using TinyMCE and had placed the files in the static folder. Everything worked on my dev server, but moved to test and prod and static .html files related to TinyMCE couldn't be found.
My web host had me add a symlink basically I think hardcoding to the server software (nginix in this case) the web address to my static HTML to the server path and that worked.
I'll have to check out the mimetypes thing, though, too.