Giter Club home page Giter Club logo

django-rat's Introduction

Creating a Django REST API running on Openshift

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

Running on OpenShift

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

Running local

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

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'

Further steps

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:

http://127.0.0.1:8000/admin/

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

django-rat's People

Contributors

dblado avatar danmcp avatar andretietz avatar andrejtokarcik avatar nhr avatar bparees avatar banzaiman avatar gshipley avatar mrunalp avatar coldfire-x avatar mfojtik avatar lmacken avatar iepathos avatar smarterclayton avatar dveeden avatar thefotios avatar zemanel avatar kraman avatar 9034725985 avatar thesteve0 avatar jay-johnson avatar

Watchers

James Cloos avatar Anish avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.