A simple experience about RESTful API with Django.
If you want to have an experience on RESTful API with Django. This is a full guide for you.First you need to know, I did this project using a youtube video on this link. Also, you can follow steps here and use the code in the repository.
- Download python from www.python.org
- Download Django web framework from www.djangoproject.com, or using pip (Python built-in package installer).
- Create the virtual environment using Terminal: $ python3 -m venv [name]
- Goninto the virtual environment using Terminal: $ source [name]/bin/activate
- Install Django using Terminal: $ pip install django
- Create a project using Terminal: $ django-admin startproject [name]
- Go into the folder using Terminal: $ cd [project-name]
- Run django using Terminal: $ python manage.py runserver
- Create an app in teh project folder using Terminal: $ django-admin startapp [name]
- Go to python_api/python_api/setting.py, add the app-name in INSTALLED_APPS. (e.g. ‘myapp’)
- In python_api/myapp/models.py add our model. you can copy from this repo.
- Convert the model that you made, into a SQL table using Terminal: $ python manage.py makemigrations
- Do the same convertation for the app too, using Terminal: $ python manage.py makemigrations myapp
- Finish the previous step by run this command on terminal: $ python manage.py migrate
- In python_api/python_api/urls.py add our api urls. you can copy from this repo.
- In python_api/myapp/views.py add our logic API and codes. you can copy from this repo.
- Now, you can test your APIs, just don’t forget to run this command before testing: $ python manage.py runserver
- At the end you will have two folder. the folder that hold our projects code titled as the name that choose for the project, and a folder as virtual environment to hold all of dependencies.
- The project name in this repo is python_api
- The app name in this repo is myapp
Leave a Reply