Django doesn't look for static files in my app - django

I've developed a stand-alone Django app that has one static JS file. On some installations, Django development server can't find the static file. The app is installed as a Python Package with pip, and I can find the JS file in site-packages/appname/static/js/myfile.js
I've installed the Django Debug Toolbar, and when looking at the "static" panel, I see a list of "static file apps". My app is not listed there. How can I tell Django it should look at my app's static file folder, too?
I'm using Django 1.6.3, with the following static file finders:
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
The django.contrib.staticfiles app is installed.
Update: When using manage.py collectstatic, files from the app are not copied.

OK, this was stupid.
The app in question was previously part of the old project. I've changed it into an independent app on a different development machine. When I wrapped it as an independent app I deleted the myproject/myapp folder on the other machine, and used pip install to install it to the project's virtual env.
Then I used this development machine, did a git pull and pip install. Turns out this *doesn't delete the myproject/myapp folder - it just deletes all the files handled by git. .pyc files are not deleted, so the folder remained, and Django looked for static files in that folder, instead of the one in site-packages.
Removing myproject/myapp fixed this.

Related

Windows IIS 10 will not serve static files from Anaconda / Django 2.2 . . . How can I fix this?

The website displays to localhost without css, js, or images.
I setup Windows IIS according to this blog including the final section about static files. The methods outlined are similar to answers from this question, this question, this blog, and this youtube video on how to serve static files from django with IIS.
UPDATE: The problem may be coming from my virtual environment. This project was developed with Anaconda. When I followed the listed tutorials I simply used the paths to my conda venv and didn't think anything of it since the localhost loaded without errors. At the time python was only present on my machine in the anaconda environment, so if manage.py was able to execute and database data was accessed and displayed(without css/js) then I presumed this was not an issue. I am currently trying to interpret this blog on how to setup Anaconda for IIS 8.5 to see if I need to make additional changes to IIS 10. I have not had any luck using web.config files and have had to accomplish the same goals through IIS manager. Jalpa Panchal's comment lead me to investigate the environment.
Any ideas on what additional changes need to be made for IIS 10 to interpret an Anaconda(conda 4.8.3) developed django application?
ORIGINAL POST:
Django settings:
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, 'static') # tested in shell, it is where I expect at the same level as manage.py
The directory "static" was created with manage.py collectstatic in the expected STATIC_ROOT location with all of the static files contained
IIS Manager:
The static directory was then added as a virtual directory:
Then I removed the django handler I created from the static directory's handler mappings:
I unlocked the handlers at the root:
The django application did display properly, with static files, when I tried using Wampserver w/ Apache and mod_wsgi. Unfortunately, it appears windows IIS will be the better solution for other unrelated reasons. Not too mention Wampserver is a development server, not a production server.
I defined a path for the environment variable WSGI_LOG, the logs generated have no errors. Just states wfastcgi.py was initialized and will restart when there are changes to the files in my project
What else can I be missing that would prevent my static files from being processed?

Does the static folder need to be checked in in Django?

For static directories that are not related to my own app, but to other Django modules (at the project_name/static directory ), do they need to be checked in to version control or do they automatically generate if a Django project is checked out somewhere else?
The project-wide django static directory, $STATIC_ROOT, referenced by settings.py, should be not be in source control. Only the static directory for each app should be in source control.
In development, static files can be served by runserver.
In production, the files are collected from each installed app to the single $STATIC_ROOT via
python manage.py collectstatic
See https://docs.djangoproject.com/en/1.7/howto/static-files/.
So you should put the static directory for any app you develop in source control, but you should treat the static directory for any third-party apps the same as any other directory for those apps. I.e., if you install an app using pip, that will include its static directory if any.

Django restframework bootstrap static files

I have a regular django site, with djangorestframework (v2.3.14) serving restful api under "/api". On local box everything works fine (mac / mavericks), on remote box (Ubuntu 12) the API browser comes up but all the bootstrap stuff is missing (the page looks like it's out of 1992 prototype instead of pretty bootstrap theme i see locally).
All the pip dependencies have been upgraded and are identical. Locally running site through PyCharm, remotely it is running on WSGI.
What can I check to see what the issue is and resolve it??
I suppose that under PyCharm on your local machine you are running the development server, which serves static files directly from your apps and projects internal locations.
After every deployment into production (your WSGI server) you need to collect all static files to a single place, your STATIC_ROOT. This is a job for the django management command collectstatic, see Django docs here.
The command may look like this:
# Executing collectstatic (organize static files)
python /path/to/your/project/manage.py collectstatic --noinput
For further details you may also read Django cannot find static files. Need a second pair of eyes, I'm going crazy.
If this question doesn't help, you can quicly fix it making a link under your proyect's static folder
ln -s /your_env_folder/lib/pythonX.X/site-packages/rest_framework/static/rest_framework rest_framework

Django: where are packages installed

I am quite a django n00b, but I am reading the eyes out of my head to get it all going. I have a PHP background and struggle with the way and location of reusable apps.
I thought that installed apps should go in an App folder (example django-registration or django-profiles), but after I PIP the app in my virtualenv, I see that the app is installed in a Django folder names "site packages".
Is this the default behavior? Should I copy the 'registration' or 'profile' folder from site packages to my Project? or should I leave them there
Thanks for the help.
If you're intending to simply install packages and not amend their code, there's no problem with them living in Python's site-packages dir.
Because you're using virtualenv, the packages installed while that virtualenv is active will be stored in:
/path/to/virtualenvs/myvirtualenv/lib/python2.x/site-packages/
And it's completely fine for them to stay there. As Daniel R says, what matters is that they are your PYTHONPATH, and virtualenv takes care of making sure they are.
Custom apps you write go in your project. Installed apps you just want to import from into your custom apps can stay in the site-packages folder.
This has nothing to do with Django. This is where Python installs packages. Django doesn't care where they are, as long as they're on the Pythonpath (which they are if they're in site-packages).

How can I emulate the paths to my Django project on my dev environment to be the same as production?

I need to setup a Django dev environment.
I did a git clone and pulled all the django project files from production on my local machine (a Vagrant enabled VM).
The problem is that my local machine has a different path to the project than my production ( and I can't change that) so it's having problems finding modules stated under INSTALLED_APPS on my local machine.
For example on the production my project is on the /myproject folder while on my local machine is under /vagrant/web/myproject.
On the production I'm accessing my app modules like this:
INSTALLED_APPS = ( 'myproject.myapp')
Also within the Django apps I'm accessing various app modules like this:
from myproject.myapp.models import *
What do I need to do to emulate production paths to my modules on my dev box so I don't have to change the paths to the modules on my local machine?
If you're doing project-relative imports, all you need to do is ensure that the path directly above your project is on the PYTHONPATH.
You need only issue the following at the command line:
export PYTHONPATH='/vagrant/web'
If you're using virtualenv, you can add that line to your environment's bin/activate file.
The path to your application directory should not matter there as long as you dont have hard-coded paths in settings.py, what web server are you using?
In your settings.py:
import os
prj_root = os.path.realpath(os.path.dirname(__file__))
And prj_root will be path to your root project folder