I have recently begun making a Django project on PythonAnyhwere.
I have followed the entire tutorial provided by Django for including static files on the project. I have observed the programs work fine on a site with http, but does not work on a site with https.
What could be the reasons and how can I overcome this problem?
Edit : The site is working now, apparently, but I would still like an explanation as to why it WAS working differently, if anyone can provide.
to load your CSS files (or totally static files), you need to follow the steps below:
1- inside settings.py
STATIC_URL = '/static/'
STATIC_ROOT=os.path.join(BASE_DIR,'static')
MEDIA_ROOT=os.path.join(BASE_DIR,'media')
MEDIA_URL='/media/'
2-collect static files using bash console
python manage.py collectstatic
3-you need to enter the Static files directory inside the web tab
check the image below
static files python anywhere
4- reload your web app
Related
I am trying to setup an AS2 server using the python package django-pyas2 (https://github.com/abhishek-ram/django-pyas2)
Everything has been working fine while I was using the runserver command, but when trying to host my web app using IIS(10), I've noticed that none of my static files get loaded when loading a page.
I've been on countless forums/documentations and I know that I need to setup my IIS to serve these files, but I haven't been able to answer a rather stupid question:
Where is the /static/ folder of django-pyas2 located ???
There is no such folder on the github page.
When using the runserver command, django somehow manages to find all the static files (.css, .js, .png) from the static folder, but I am still totally clueless to where that folder is actually stored.
I've been searching in these locations to no avail: (I'm using a virtual env on Windows Server 2016)
my own project
my venv's site-packages\django-pyas2 folder
in C:\Python37\Lib\site-packages\django-pyas2
in C:\User\AppData\Roaming\Python\Python37\Lib\site-packages\django-pyas2
I've also tried to use the command collectstatic, but I first need to know where the actual /static/ folder is stored.
I've double-checked, and the files are apparently stored locally and not on a CDN. This is driving me kind of nuts, and I could definetly use some help from someone!
A big thanks in advance ❤
EDIT:
my IIS wfastcgi configuration
my IIS advanced settings
I'm not sure about how IIS(10) works but this is usually the fix.
go to settings.py and add a path for your static files.
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')#new
Next on the console (command prompt/ terminal/shell) run
python manage.py collectstatic
This should work
OK, so I finally found the static folder I was looking for in this location:
<PYTHON_PATH>\Lib\site-packages\django\contrib\admin\static
I believe the django_pyas2 package is using some default js/css already included with Django, and I couldn't find it because it was burried in Django's files.
For those encountering the same problem, here are the steps I then took to make it work with IIS(10):
copy/pasted the static folder from the django package to my project's root
went to IIS manager, right clicked on my website > Add Application
for the application alias: "static", for the path: <path_to_static_folder>
/!\ IMPORTANT /!\ left clicked on newly created "static" application, went to Mapping Manager, and then deleted the associated FastCGIModule
Didn't have to do anything in settings.py, nor execute the collectstatic command.
Works fine after that!
Thanks for trying to help.
In Django, the convention is to put all of your static files (i.e css, js) specific to your app into a folder called static. So the structure would look like this:
mysite/
manage.py
mysite/ --> (settings.py, etc)
myapp/ --> (models.py, views.py, etc)
static/
In mysite/settings.py I have:
STATIC_ROOT = 'staticfiles'
So when I run the command:
python manage.py collectstatic
It creates a folder called staticfiles at the root level (so same directory as myapp/)
What's the point of this? Isn't it just creating a copy of all my static files?
Collect static files from multiple apps into a single path
Well, a single Django project may use several apps, so while there you only have one myapp, it may actually be myapp1, myapp2, etc
By copying them from inside the individual apps into a single folder, you can point your frontend web server (e.g. nginx) to that single folder STATIC_ROOT and serve static files from a single location, rather than configure your web server to serve static files from multiple paths.
Persistent URLs with ManifestStaticFilesStorage
A note about the MD5 hash being appended to the filename for versioning: It's not part of the default behavior of collectstatic, as settings.STATICFILES_STORAGE defaults to StaticFilesStorage (which doesn't do that)
The MD5 hash will kick in e.g. if you set it to use ManifestStaticFilesStorage, which adds that behavior.
The purpose of this storage is to keep serving the old files in case
some pages still refer to those files, e.g. because they are cached by
you or a 3rd party proxy server. Additionally, it’s very helpful if
you want to apply far future Expires headers to the deployed files to
speed up the load time for subsequent page visits.
Django static files can be in many places. A file that is served as /static/img/icon.png could come from many places. By default:
FileSystemFinder will look for img/icon.png in each of STATICFILES_DIRS,
AppDirectoriesFinder will look for img/icon.png in the static subfolder in each of your INSTALLED_APPS. This allows libraries like Django Admin to add their own static files to your app.
Now: this only works if you run manage.py runserver with DEBUG=1. When you go live, the Django process will no longer serve the static assets. It would be inefficient to use Django for serving these, there are more specialised tools specifically for that.
Instead, you should do something like this:
find all of static files from every app
build a single directory that contains all of them
upload them somewhere (a static directory somewhere on your webserver or a third-party file storage)
configure your webserver (such as nginx) to serve /static/* directly from that directory and redirect any other requests to Django.
collectstatic is a ready-made script that prepares this directory for you, so that you can connect it directly to your deployment script.
In the production installation, you want to have persistent URLs. The URL doesn't change unless the file content changes.
This is to prevent having clients to have wrong version of CSS or JS file on their computer when opening a web page from Django. Django staticfiles detects file changes and updates URLs accordingly, so that if CSS or JS file changes the web browser downloads the new version.
This is usually achieved by adding MD5 hash to the filename during collectstatic run.
Edit: Also see related answer to multiple apps.
It's useful when there are multiple django apps within the site.
collectstatic will then collect static files from all the apps in one place - so that it could be served up in a production environment.
Most of my static files on my newly deployed Django website are working (CSS), but not the images. All the images are broken links for some reason and I cannot figure out why. I am serving my static files via Amazon AWS S3.
I believe all my settings are configured correctly as the collectstatic command works (and the css styling sheets are up on the web). What could be the problem?
It's probably impossible to give an accurate assessment with the limited info on your setup. If your css files are working what folder are they sitting in on your server?
Why not have images folder in the same directory and set that directory to your MEDIA_URL in your settings.py file?
In your browser check your images full path and compare that to your CSS files, where are they pointing, do you have a directory on your server where they are supposed to be? are you receiving an access denied if you try to directly put in that image url into your browser?
I am reformulating a previous question because I think it was formulated as a Python problem but it is a Django one.
I am installing a project from github called publicmarkup:
The main page displays on the browser but no media (neither js files, nor css files) are read loaded. There is a module called mediasync that seems to be necessary to display correctly the css and the js files of the templates.
I think this a configuration problem. Here's the link settings.py file, Here it could be find the root path of the media folder.
And here's the doc for the mediasync modulet explains how to configure static_url
But I didn't understand anything since I am a newbie to djnago
In Django you have to set up your own workstation serve static files, in both dev and live modes.
You can read about it here.
This explains how to serve static files in your dev environment.
In live mode you serve your static files directly via your end web server, usually either Apache or Nginx.
I successfully deployed my Django site to IIS7 but I still have problems about how to configure IIS to serve the static files. I tried many many things from the internet but nothing seems to work. Everything is working fine on Django server (manage.py runserver) and with debug = True, but as soon as I turn off debug (debug = False) and open it on IIS, the bootstrap css and js are not loaded.
Do you have any ideas about why I'm experiencing this behavior? Perhaps you could point me to a step-by-step guide to help me?
Right click on your website in IIS7 manager and
add a virtual directory
name it with the same name of your folder you want IIS to handle. lets say static
add the path to your real static folder in your application mine was in myproject/static then ok
and here you go :)
If you are using django >= 1.3 and following the doc you are probably using the 'staticfiles' app.
First, you must configure your II7 to serve static files from the chosen path, by default URL: /static/ and PATH /staticfiles/ (I have no experience with II7 but the conf should be straightforward)
Then run ./manage.py collectstatic to move the static files into the correct path
and you should be done...
More info on production settings here.
You also need to insert a web.config file into the static directory for IIS to serve the files.
See: Django Static Files - 404