You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
5.3 KiB
5.3 KiB
Django REST Framework Contacts API Template
Description
This is a single model full CRUD API for contacts using Python and Django.
This is an api only. I would suggest consuming this API using React as a separate server by using create-react-app.
Contacts Properties:
| Property | Type | Default |
|---|---|---|
| id | integer | assigned by db |
| name | string | n/a |
| age | int | n/a |
Contacts Routes:
| Endpoint | Action |
|---|---|
/api/contacts |
GET (Index) |
/api/contacts/:id |
GET (Show) |
/api/contacts |
POST |
/api/contacts/:id |
DELETE |
/api/contacts/:id |
PUT |
System Requirements
- Python 3
- pipenv
Installation
If you don't have either of the above, please install them.
Python3 Installation
Python Installation
- Check what python version you have on your computer by running:
python -V - If you're not on a version of Python that is 3 or greater, install python 3 with homebrew:
brew install python3- Note: in order to use this installed python3, you will have to use
python3whenever running a python command
pipenv Installation
pipenv Installation
To build your app, we're going to be building a virtual environment. In order to manage our dependencies and our virtual environment, we're going to use pipenv.
- Check if you have pipenv by running:
pipenv --version - If you do not have it, install it with homebrew:
brew install pipenv
Get Set Up Locally
On your Browser
- Fork this repository to your account
In your Terminal
- Clone your fork of the repo onto your computer anywhere that is not a git repo
cdinto the repo- Touch a
.envinto the root of your project and add a SECRET_KEY value. See the .env.sample file for an example - Install all the required packages by running:
pipenv install - Activate the virtual environment by running:
pipenv shell- NOTE: To exit the shell gracefully whenever you're done working, use
exit
- NOTE: To exit the shell gracefully whenever you're done working, use
- Create the
django_contactspsql database and user by running the following while in the pipenv shell:psql -U postgres -f settings.sql- You can find the database name and username/password inside the settings.sql file
- Apply the migrations by running the following while in the pipenv shell:
python3 manage.py migrate - Make a superuser for your app, this will allow you to work directly with your database on the browser without having to use Postman
- In the pipenv shell, run
python3 manage.py createsuperuserand follow the instructions
- In the pipenv shell, run
- Start the Django server by running the following inside the pipenv shell:
python3 manage.py runserver
On your Browser
- Go to
localhost:8000/api/contacts. You should see the Django REST Framework interface like so:
- Click the
Log Inon the top right corner and use the username/password you created when making the superuser - You should now be able to add / delete / edit / read contacts directly from your browser
Making Changes
Updating / Creating Models
- If you make any changes to the
contacts_api/models.pyfile, you will need to make and run a migration to apply the changes. You can do so by using the following commands while inside the pipenv shell- Make the migration:
python3 manage.py makemigrations - Apply the migration:
python3 manage.py migrate
- Make the migration:
Set Up for Heroku Deployment
In Terminal
- Create a heroku app from the root of your project folder, run:
heroku create- The above command will randomly generate a name for you, if you want to name your app something specific run:
heroku create urlNameYouWantHere
- The above command will randomly generate a name for you, if you want to name your app something specific run:
In your Code Editor
- Copy the heroku url that was created (without the
https://), go to yourdjango_rest_api/settings.pyand add it into theALLOWED_HOSTS
On the Browser
- Go to your heroku dashboard for the heroku project you just created
- Click on Configure Add-Ons
- Search for Heroku Postgres and add it
- Go to the Settings, Reveal Config Vars, add a config var for SECRET_KEY and anything else you have in your .env file
In Terminal
pipenv lockto ensure your pipfile.lock is up to dategit add -Agit commit -m "heroku deployment"git push heroku master- Once it builds successfully, run
heroku run bash - While in heroku bash, apply the migrations to the heroku project by running:
python manage.py migrate - Still in heroku bash, create a superuser for the heroku project by running
python manage.py createsuperuserand follow the prompts- To exit heroku bash, run
exit
- To exit heroku bash, run
In Browser
- After the migrations finish, you should now be able to open the heroku app and use this deployed version as your backend API
- Don't forget to go to
/api/contacts
- Don't forget to go to
- Remember that your heroku database is separate from your local database, so there should not be any data on the first load.
- You can add data by logging in with the heroku superuser you created
