I was trying to reproduce the tutorial Django app and uploading it to Heroku server, but I can't resolve problems with static files.
Here is a link to all files on github:
https://github.com/Rufus90/poll.git
When I try to run
heroku run python manage.py collectstatic --noinput
I get this error:
Running python manage.py collectstatic --noinput on ⬢ hidden-plains-30510... up, run.1265 (Free)
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 188, in handle
collected = self.collect()
File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 105, in collect
for path, storage in finder.list(self.ignore_patterns):
File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/finders.py", line 131, in list
for path in utils.get_files(storage, ignore_patterns):
File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/utils.py", line 23, in get_files
directories, files = storage.listdir(location)
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/files/storage.py", line 315, in listdir
for entry in os.scandir(path):
FileNotFoundError: [Errno 2] No such file or directory: '/app/static'
After completing the tutorial I have copied the whole repo, renamed it and changed the paths where I think it was needed. I was playing with my old project from tutorial but it got completely messed up and stopped working at all so I stuck to this one.
Right now I can't even replace old static files with new ones or change font or anything and I don't know where the problem lies.
Can someone please explain me what did I do wrong? Besides copying the folder and renaming it..
As per heroku documentation, you need to create that directory /app/static inside your code base, so that it is available when you run collectstatic. Here is what documentation has mentioned:
Django won’t automatically create the target directory (STATIC_ROOT)
that collectstatic uses, if it isn’t available. You may need to create
this directory in your codebase, so it will be available when
collectstatic is run. Git does not support empty file directories, so
you will have to create a file inside that directory as well.
Alternatively you can use whitenoise in heroku. As django does not serve static files in production mode(when you set DEBUG=False in settings), here whitenoise becomes handy. It can serve static files in production mode. Integrating it in your django project is easy as well.
First, install it using pip install whitenoise.
Then add WhiteNoiseMiddleware in settings.py:
MIDDLEWARE_CLASSES = (
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
'whitenoise.middleware.WhiteNoiseMiddleware',
...
Finally if you want GZIP enabled storage for static files then put the following line in settings.py file:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
More info on whitenoise can be found here.
Related
I'm trying to setup a Django project and am faced with the following error:
WARNINGS:
?: (rest_framework.W001) You have specified a default PAGE_SIZE pagination rest_framework setting,without specifying also a DEFAULT_PAGINATION_CLASS.
HINT: The default for DEFAULT_PAGINATION_CLASS is None. In previous versions this was PageNumberPagination. If you wish to define PAGE_SIZE globally whilst defining pagination_class on a per-view basis you may silence this check.
Traceback (most recent call last):
File "manage.py", line 18, in <module>
execute_from_command_line(sys.argv)
File "/Users/george/.virtualenvs/mixapi/lib/python3.7/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/Users/george/.virtualenvs/mixapi/lib/python3.7/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/george/.virtualenvs/mixapi/lib/python3.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/george/.virtualenvs/mixapi/lib/python3.7/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/Users/george/.virtualenvs/mixapi/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 82, in handle
executor.loader.check_consistent_history(connection)
File "/Users/george/.virtualenvs/mixapi/lib/python3.7/site-packages/django/db/migrations/loader.py", line 291, in check_consistent_history
connection.alias,
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration events.0001_initial is applied before its dependency commons.0001_initial on database 'default'.
make: *** [migrate] Error 1
I am running this from within a virtual environment, and have a docker-compose container for a postgres db running. Not sure how to start debugging this or why its happening. Any help would be appreciated
As per your snapshot:
you need to run command:- python manage.py migrate commons and then migrate for events model.
Follow these steps in your migrations folder inside the project:
Delete the pycache and the 0001_initial files.
Delete the db.sqlite3 from the root directory (be careful all your data will go away).
on the terminal run:
python manage.py makemigrations
python manage.py migrate
Note: don't forget to take a backup of your data.
For more detail, you can follow the link
I am trying to deploy Django following this tutorial but am getting an error. When the tutorial says to run python manage.py collectstatic, instead of collectstatic running without further prompt as suggested, I get the following:
/home/elections/venv/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
You have requested to collect static files at the destination
location as specified in your settings.
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
I entered Yes and got the following error:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/elections/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/home/elections/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/elections/venv/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/elections/venv/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/elections/venv/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 199, in handle
collected = self.collect()
File "/home/elections/venv/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 124, in collect
handler(path, prefixed_path, storage)
File "/home/elections/venv/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 354, in copy_file
if not self.delete_file(path, prefixed_path, source_storage):
File "/home/elections/venv/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 260, in delete_file
if self.storage.exists(prefixed_path):
File "/home/elections/venv/lib/python3.6/site-packages/django/core/files/storage.py", line 392, in exists
return os.path.exists(self.path(name))
File "/home/elections/venv/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 50, in path
raise ImproperlyConfigured("You're using the staticfiles app "
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
What could have made this happen differently from the tutorial, and how do I fix this error?
As already mentioned you need to define the STATIC_ROOT variable in your settings.py in order to make collectstatic work.
The absolute path to the directory where collectstatic will collect
static files for deployment.
Example: "/var/www/example.com/static/"
You may want to define STATICFILES_DIRS too.
The nginx.conf from the tutorial you have posted expects to serve your collected static files from /home/boards/staticfiles/. Since the tutorial deploys everything on a single, monolithic server which hosts the database (postgres), web server (nginx) and the application (django) you can set it as follows:
STATIC_ROOT = '/home/boards/staticfiles/'
I am trying to write a script to start the server in my production environment (using apache mod_wsgi):
#!/bin/bash
#activate virtualenv
source ~/.virtualenvs/myproj/bin/activate
# install possibly changed requirements
echo "Checking changed dependencies..."
pip3 install --no-cache-dir -r /opt/myDjangoProj/requirements.txt
deactivate
# run server
echo "Starting django server ..."
#export DJANGO_SETTINGS_MODULE='myDjangoProj.settings.production'
~/.virtualenvs/myproj/bin/python3 /opt/myDjangoProj/manage.py runserver 8000 --settings myDjangoProj.settings.production
However, I alwas get this stacktrace: (installing requirements works fine, so I omit it)
Starting django server ...
Traceback (most recent call last):
File "/opt/myDjangoProj/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/localadm/.virtualenvs/myproj/lib/python3.2/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/localadm/.virtualenvs/myproj/lib/python3.2/site-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/home/localadm/.virtualenvs/myproj/lib/python3.2/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/localadm/.virtualenvs/myproj/lib/python3.2/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/tmp/pip-build-w8vru4/Django/django/contrib/admin/apps.py", line 22, in ready
File "/tmp/pip-build-w8vru4/Django/django/contrib/admin/__init__.py", line 24, in autodiscover
The directory /tmp/pip-build-w8vru4/ does not actually exist. Do you have any ideas what the problem might be?
First thing is generally we don't need shell scripts when we are using mod_wsgi.
refer https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/modwsgi/ for setting up django with mod_wsgi
Second your working directory not seams to be django project root. switch to django project root or include it in PYTHONPATH.
When I run a custom command in Django 1.8.3, I'm getting the following stack trace:
(env)MacBook-Air:backend user$ python manage.py questions
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/user/WeVote/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Users/user/WeVote/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/user/WeVote/env/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/user/WeVote/env/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/Users/user/WeVote/wevote/questions/management/commands/questions.py", line 14, in handle
OSError: [Errno 2] No such file or directory: '/Users/user/WeVote/backend/questions/question_files'
The error is valid, that filepath doesn't exist. I go to look at line 14 in questions.py, except that file doesn't exist. Boo.
As you can see, the file structure is different in referencing questions.py file. As some point, I'm sure that was the file structure. I also wrote this in Djano 1.7 and upgraded to 1.8. However, all the other custom commands (written both before and after the upgrade) are working just fine.
Just as a sanity check, I tried a
cd /Users/user/WeVote/wevote/questions/management/commands/
Nope. This is an issue because when I now edit my properly-located questions.py in the management/commands directory, the changes aren't reflected. It's like it's reading a cached version and location of this file.
Also tried deleting and rebuilding both virtualenv and database. No love on either. I'm sure it's something simple I'm overlooking, but I'm out of ideas.
Reference for anyone coming afterwards: The comments from yuvi and Paul Collingwood above succeeded in solving my issue. I ran
find . -name '*.pyc' -delete
and the next time I ran questions.py everything worked well. Thanks both for your comments.
I have Windows 7 and Python 2.7 with setuptools installed.
After i download pitz module (easy_install pitz), Google App Engine stop working:
bad runtime process port ['']
Traceback (most recent call last):
File "G:\Program Files (x86)\Google\google_appengine\_python_runtime.py", line 184, in <module>
_run_file(__file__, globals())
File "G:\Program Files (x86)\Google\google_appengine\_python_runtime.py", line 180, in _run_file
execfile(script_path, globals_)
File "G:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\python\runtime.py", line 28, in <module>
from google.appengine.ext.remote_api import remote_api_stub
File "G:\Program Files (x86)\Google\google_appengine\google\appengine\ext\remote_api\remote_api_stub.py", line 75, in <module>
import yaml
File "G:\Program Files (x86)\Google\google_appengine\lib\yaml-3.10\yaml\__init__.py", line 14, in <module>
from cyaml import *
File "G:\Program Files (x86)\Google\google_appengine\lib\yaml-3.10\yaml\cyaml.py", line 5, in <module>
from _yaml import CParser, CEmitter
File "C:\Python27\lib\site-packages\pyyaml-3.11-py2.7-win-amd64.egg\_yaml.py", line 7, in <module>
File "C:\Python27\lib\site-packages\pyyaml-3.11-py2.7-win-amd64.egg\_yaml.py", line 4, in __bootstrap__
File "C:\Python27\lib\site-packages\pkg_resources.py", line 950, in resource_filename
self, resource_name
File "C:\Python27\lib\site-packages\pkg_resources.py", line 1607, in get_resource_filename
self._extract_resource(manager, self._eager_to_zip(name))
File "C:\Python27\lib\site-packages\pkg_resources.py", line 1667, in _extract_resource
manager.extraction_error()
File "C:\Python27\lib\site-packages\pkg_resources.py", line 996, in extraction_error
raise err
pkg_resources.ExtractionError: Can't extract file(s) to egg cache
The following error occurred while trying to extract file(s) to the Python egg
cache:
[Error 5] : 'C:\\Users\\Kostr\\AppData\\Roaming\\Python-Eggs\\pyyaml-3.11-py2.7-win-amd64.egg-tmp\\_yaml.pyd'
The Python egg cache directory is currently set to:
C:\Users\Kostr\AppData\Roaming\Python-Eggs
Perhaps your account does not have write access to this directory? You can
change the cache directory by setting the PYTHON_EGG_CACHE environment
variable to point to an accessible directory.
How to solve this issue?
from: https://code.google.com/p/modwsgi/wiki/ApplicationIssues
To avoid this particular problem you can set the PYTHON_EGG_CACHE cache environment variable at the start of the WSGI application script file. The environment variable should be set to a directory which is owned and/or writable by the user that Apache runs as.
import os
os.environ['PYTHON_EGG_CACHE'] = '/usr/local/pylons/python-eggs'
Again, make sure this exists. For Windows users, maybe something like:
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
Alternatively, if using mod_wsgi 2.0, one could also use the WSGIPythonEggs directive for applications running in embedded mode, or the python-eggs option to the WSGIDaemonProcess directive when using daemon mode.
Note that you should refrain from ever using directories or files which have been made writable to anyone as this could compromise security. Also be aware that if hosting multiple applications under the same web server, they will all run as the same user and so it will be possible for each to both see and modify each others files. If this is an issue, you should host the applications on different web servers running as different users or on different systems. Alternatively, any data required or updated by the application should be hosted in a database with separate accounts for each application.
Sounds like you don't have permissions to write to the directories. Uninstall pitz, and reinstall using the -Z tag to install it as the unzipped version. Then, you don't need the cache directory to extract each time. That should solve the directories / permissions issue.