This is the first time I am trying to deploy my django project (myproject) on Webfaction.
My project dir-structure is as follows:
In webapps/django: myproject.wsgi, myproject
settings.py is at myproject/src/myproject/
Under such circumstances, how should I define DJANGO_SETTINGS_MODULE in myproject.wsgi?
For the default installation by webfaction, it is defined as myproject.settings. Should I be defining DJANGO_SETTINGS_MODULE as myproject.src.myproject.settings?
When you setup a Django project on webfaction, you get a file structure like so:
~ (your home directory)
+webapps
+some_project_name
+myproject
-standard django files
-settings.py
-app_directory
+apache2
+bin
-start
-stop
-restart
-other dirs
-bin
-lib
-myproject.wsgi
-some_project2
-symlink_to_static_files
If you are building your project under "myproject", you should not have to modify the wsgi file to get started- just change to the apache2/bin directory and run ./start and you're good to go!
If you change the wsgi file you'll need to run ./stop then ./start to activate the changes.
If the path you listed isn't working, it may be worth trying to create a generic django project and just puaste your project right over 'myproject'
It seems like your service provider has already given you some helpful instructions:
http://docs.webfaction.com/software/django/getting-started.html
Related
How to run Django-simple-blog? I tried to install django-simple-blog but could not find the manage.py file to run it. Can I get a solution or another simple blog?
Django has a concept of apps and a concept of projects. A project will have a manage.py file like you mention, and will also have a settings.py file that declares all of the apps that the project uses.
django-simple-blog is an app, meaning you install it within an existing project. After this explaination, the rest of the steps found here should be easier to follow: https://github.com/drager/django-simple-blog/blob/master/README.rst
The remaining steps are to:
Add 'simpleblog' to INSTALLED_APPS in your settings.py file
run the command python manage.py migrate from your project root
include 'simpleblog.urls' into any of your urls.py file
I have an existing application in Django.
I want to add a translation on the page.
On page I have:
{% trans 'Projects'%}
In .po file I added:
#: templates/staff/site.html: 200
msgid "Projects"
msgid "Projekty"
Then executes the command:
django-admin.py compilemessages -l pl
After this command, I get an error:
CommandError: This Should Be Run script from the Django Git checkout or your project or app tree, or with the settings Specified module.
$ python manage.py compilemessages --settings nsp.settings
CommandError: This script should be run from the Django Git checkout or your project or app tree, or with the settings module specified.
I have got this error while I truly was inside project root folder.
The problem was, that I was running this command without python manage.py makemessages first.
The error message is misleading.
The error holds the answer, you could be running the script from anywhere so it cannot know which files to compile. Run the command from the project directory or specify the settings and you should be fine.
If you are using docker containers to build and deploy your application you should copy folder:
conf/
from root folder of your django project.
with the conf folder you should see i.e:
processing file django.po in /gamma/conf/locale/en/LC_MESSAGES
processing file django.po in /gamma/conf/locale/es/LC_MESSAGES
processing file django.po in /gamma/conf/locale/pt_BR/
without the conf folder you should see a clueless message like that:
CommandError: This script should be run from the Django Git checkout or your project or app tree, or with the settings module specified.
The error message is saying that it could not find the translations files where it expected them to be. Check that everything is correctly setup:
LOCALE_PATHS is defined in your settings.py
the files exist in the folder defined above (created by running python manage.py makemessages)
actually the error goes away even with just an empty locales folder
the compilemessages command is run from the project root folder
If you haven't set LOCALE_PATHS in your settings file, you need to do so:
import os
LOCALE_PATHS = [os.path.join(BASE_DIR, 'locale')]
I'm following the approach in Two Scoops of Django: Best Practices for Django 1.6 regarding multiple settings files. I'm using Django 1.7 and virtualenvwrapper.
My setup is as follows:
project/
app1/
app2/
project/
__init__.py
settings/
__init__.py
base.py
local.py
production.py
manage.py
I'm a bit confused as to how Django knows which settings file to use. I do not want to specify the settings file every time I run manage.py. I would rather like to set the DJANG_SETTINGS_MODULE environmental variable as explained in omouse anser here:
What confuses me is in the wsgi.py file there is a line:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings.production")
Is this file only used in the production server? What happens if I already have a DJANGO_SETTINGS_MODULE environmental variable defined on the server?
When running it locally, I understand I need to set the DJANGO_SETTINGS_MODULE env variable every time I open the console. I've read here that I can define a postactivate hook in virtualenvwrapper. This hook will then create the environmental variables that I require everytime I activate the environment.
Is this the recommended way of ensuring the correct DJANGO_SETTINGS_MODULE env variable is loaded on my local machine? Would I also need to setup a similar file on my hosting server? I'm planning on using PythonAnywhere for hosting.
Lastly, if I run a staging server, how would I tell Django to load the staging settings file? The staging server is the practically the same as the production server, so I guess need a different wsgi.py file for the staging server, but that seems like a anti-pattern.
os.environ.setdefault only sets the value if it is not set. When you run in production, export the environment variable DJANGO_SETTINGS_MODULE and set it to your production/staging settings file, and you don't have to set anything when running in development (if you set it by default to your development settings). This is the DRY-est method.
The method with a local_settings.py (which is most of the times kept out of the repo!) is not best practice and should be avoided.
After successfully deploying a test app using the steps outlined here:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Python_flask.html
I tried to deploy my actual flask application which has the following structure:
myApp/
runServer.py
requirements.txt
myApp/
__init__.py
helpers.py
clean.sh
static/
myApp.css
handlers/
__init__.py
views.py
templates/
layout.html
viewOne.html
viewTwo.html
Where views.py contains my url mappings.
I have tried initializing the eb instance in the root directory as well as within the myApp module and git aws.push but I get the following error on the AWS dashboard:
ERROR Your WSGIPath refers to a file that does not exist. and the application does not work (404 for any path).
How can I deploy the above Flask application to elastic beanstalk?
I encountered a similar problem deploying a Flask application to EB, with a similar directory structure, and had to do 2 things:
Update my manage.py to create an object of name application, not app
import os
from application import create_app, db
from flask.ext.script import Manager, Shell
application = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(application)
Create .ebextensions/myapp.config, and define the following block to point to manage.py
option_settings:
"aws:elasticbeanstalk:container:python":
WSGIPath: manage.py
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "application/static/"
This let Elastic Beanstalk find the application callable correctly.
This is described briefly at the official docs, and is described in more detail in this blog post
EDIT - see project structure below
ProjectRoot
.ebextensions
application.config
application
main
forms.py
views.py
static
templates
tests
manage.py
requirements.txt
config.py
etc, etc
Add the following to .ebextensions/<env-name>.config:
option_settings:
"aws:elasticbeanstalk:container:python":
WSGIPath: myApp/handlers/views.py
Update:
If you don't have .ebextensions directory, please create one for the project. You can find more information of what can be done regarding the container configuration in Customizing and Configuring AWS Elastic Beanstalk Environments guide.
Your WSGIPath refers to a file that does not exist.
This error appears because Beanstalk, by default, looks for application.py. Check at Beanstalk web UI, Configuration > Software Configuration, WSGIPath is mapped to application.py
Update the WSGIPath as shown in the previous replies or rename to application.py file.
As of awsebcli 3.0, you can actually edit your configuration settings to represent your WSGI path via eb config. The config command will then pull (and open it in your default command line text editor, i.e nano) an editable config based on your current configuration settings. You'll then search for WSGI and update it's path that way. After saving the file and exiting, your WSGI path will be updated automatically.
WSGI configuration was painful for me. I did changed WSCI settings using eb config command but it did not work. Below you can fix this in 5 easy steps.
1- Moved app.py function to the root of the directory (where I runned eb init command.
2- Also renamed app.py as application.py and in that initilized application as application = Flask(__name__) not app = Flask(__name__)
3- eb deploy did not worked after this (in the same project) I tried to fix config by using eb config but it was too hairy to sort it out. Delete all .extensions, .gitignore etc from your project.
4- re initialize your project on EB with eb init and follow the prompts. when deployment is done, eb open would launch your webapp (hopefully!)
When I encountered this problem it was because I was using the GUI to upload a zip of my project files. Initially I was zipping the project level directory and uploading that zip to EB.
Then I switched to simply uploading a zip of the project files themselves-ie select all files and send those to a zip-and then the GUI upload utility was able to find my application.py file without a problem because the application.py file was not in a subfolder.
Well, In my case I followed the entire process and conventions but was still getting 404. The problem was my virtual environment. I was ignoring all environment config related folders/files in my .gitignore but not in .ebignore. After creating .ebignore and ignoring all the folders/files which were not related to project code, fixed the issue.
A note before the question
We generally have a settings.py file and a local_settings.py file in standard Django project layouts.
settings.py: for production settings
local_settings.py: for local settings that override production settings when running the project locally
The local_settings.py file is added to .gitignore to avoid being pushed into production via git push.
The question
In appengine when we push the application using:
appcfg.py update exampleproject
The local_settings.py file also gets pushed up even though it has been added to .gitignore.
At first adding the following lines to app.yaml looks like a possible solution:
skip_files:
- ^(.*/)?local_settings.py
These lines tell app engine to ignore the local_settings.py file.
But, then we face another problem ->
The local_settings.py file is totally excluded. ie. It is not even used when running the application locally along with dev_appserver.py.
Is there any good solution that can help define multiple settings files in a Google App Engine based Django project?