This git repository helps you get up and running quickly w/ a Django installation on OpenShift. The Django project name used in this repo is 'restapi' but you can feel free to change it. Right now the backend is sqlite3 if it is not running on Openshift (i.e. locally).
With this fork I want to be able to easily create a REST API that is usable with mobile devices. I'll try to create a demoproject for that soon.
After pushing this the first time, you have to ssh to your Openshift a (if you have multiple, it does not matter in which of course) ec2 instance and run:
$ $OPENSHIFT_REPO_DIR/wsgi/restapi/manage.py createsuperuser
to create a superuser for the user backend.
On subsequent pushes, a python manage.py syncdb
is executed to make
sure that any models you added are created in the DB. If you do
anything that requires an alter table, you could add the alter
statements in GIT_ROOT/.openshift/action_hooks/alter.sql
and then use
GIT_ROOT/.openshift/action_hooks/deploy
to execute that script (make
sure to back up your database w/ rhc app snapshot save
first :) )
You can also turn on the DEBUG mode for Django application using the
rhc env set DEBUG=True --app APP_NAME
. If you do this, you'll get
nicely formatted error pages in browser for HTTP 500 errors.
Do not forget to turn this environment variable off and fully restart the application when you finish:
$ rhc env unset DEBUG
$ rhc app stop && rhc app start
Create an account at https://www.openshift.com
Install the RHC client tools if you have not already done so:
sudo gem install rhc
rhc setup
Create a python application
rhc app create django python-2.7
Add this upstream repo
cd django
git remote add upstream -m master https://github.com/Unic8/django-rest-api-template.git
git pull -s recursive -X theirs upstream master
Then push the repo upstream
git push
That's it. You can now checkout your application at:
http://django-$yournamespace.rhcloud.com
Make sure python is installed, you can find and download it on https://www.python.org/download/. Install pip, which you can find on https://pip.pypa.io/en/stable/installing.html
Install django using: On *nix:
sudo pip install django
On Windows open the shell with administrator privileges (Shift+Rightclick on the icon) and execute:
pip install django
Install all dependencies, you can find in the setup.py using pip
pip install djangorestframework
pip install markdown
pip install django-filter
pip install django-rest-auth
pip install django-allauth
Clone this repository and call
repodir/wsgi/restapi/manage.py migrate
repodir/wsgi/restapi/manage.py createsuperuser
This will create a local sqlite database file, after that you create the superuser
Setup the Email-Configurations in the settings file
...
if ON_OPENSHIFT:
# On openshift we want an actual smtp server
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.example.com'
EMAIL_PORT = 557
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'somesupersecretpassword'
else:
...
Setup the login redirect url:
LOGIN_REDIRECT_URL = 'https://github.com/Unic8'
Try to login to your user backend with the user credentials you entered while creating the superuser.
http://django-$yournamespace.rhcloud.com/admin/
or if you're running locally:
The you could go into "Sites" and change example.com to your Application domain (or 127....)
Logout and try to open:
http://domain/auth/registration/
you should see the default BrowsableAPI for the registration, which tells you that you can't call this using GET (default by the browser of course)
Enter a testuser like (min 6 password characters):
{"username":"testuser", "password1":"asdasd", "password2":"asdasd", "email":"[email protected]"}
Press on POST to register this user. If that was successfull you should get a userobject in json in return.
Locally you should take a look at the django console. There should be an email printed on the console. On Openshift, you should receive an email (if you setup the email-settings in the settings correctly).
Open the link that is provided in the Email/Console. This should lead you to a very basic web interface, in which you can confirm the email address. After you clicked the confirm button you will be forwarded to the url you setted up in the settings.py
Finally you should be able to login via:
http://domain/auth/login/
This login returns a Token which you have to send for each further authenticated request. In each http header you need to add:
...
Authorization: Token <your-auth-token>
...
Hope that helps someone.
Things I want to add:
-
How to change the default templates (confirm email, reset password etc.)
Since this needs an actual application running in this project, I left that out for now
-
Social login support (Facebook, Google etc)
It could be that it is working already, I could not test it yet.
-
Push messaging