
Saturday, August 6, 2022
This post will help you to build a music API that serves the music. I will be choosing the Django rest framework because it is easy to build a Rest API and well-suited for Django. By the end of this tutorial, you will be having a music API with user management (login, registration) and JSON Web Tokens (JWT) to improve security. If you are barely new to API development I would suggest you read this post.
Let's get started :
For this project, I am using Python 3.6.9 and Django version 3.0.8.
Let's create a virtual environment.
Let's create a virtual environment.
python3 -m venv env
Then activate the virtual environment.
source env/bin/activate
Now Install Django, DRF (Django Rest Framework)
Register the same models for admin.py
pip install Django==3.0.8Let's create a project.
pip install djangorestframework
django-admin startproject apiWe will be using different apps for music files and Authentication. This helps us to develop the projects faster and also helps in implementing new features, without affecting the rest.
django-admin startapp music && django-admin startapp usersNow add these to INSTALLED APPS in api/settings.py file
rest_framework,Let's add music app URLs to api with versioning.
rest_framework.authtoken,
music,
users
Music API:
Navigate to the Music app and add music models.
Next, we have to serialize these models. Serializers allow complex data and model Instances to python data types that can be easily transmitted through JSON or XML. Serializers also provide great control to manipulate our output responses.
The views are processed using this serializer (read about serializers and its types here). Open the views.py file and add get and post request views.
Here queryset takes all the objects present in the Songs model and process to list view. Search fields add searching capability to the view. here we search the songs using song title. I commented on Permission classes that restrict the songs view for logged-in users only, which will be uncommented after implementing authentication.
Now let's add URLs to these views
Let's create a default folder for media (songs, user avatars ). Add these to api/settings.py.
Now let's create a superuser.
After adding navigate to songs endpoint to view the songs
http://127.0.0.1:8000/api/v1/songs/
The views are processed using this serializer (read about serializers and its types here). Open the views.py file and add get and post request views.
Here queryset takes all the objects present in the Songs model and process to list view. Search fields add searching capability to the view. here we search the songs using song title. I commented on Permission classes that restrict the songs view for logged-in users only, which will be uncommented after implementing authentication.
Now let's add URLs to these views
Let's create a default folder for media (songs, user avatars ). Add these to api/settings.py.
MEDIA_URL = '/media/'MEDIA_ROOT = os.path.join(BASE_DIR, "media_root")This makes media_root as the base directory for every media element.
Now let's create a superuser.
python manage.py makemigrationsAfter creating a superuser, run our app using
python manage.py migrate
python manage.py createsuperuser
python manage.py runserverNow add some sample songs using django admin (127.0.0.1:8000/admin/music/songs/)
After adding navigate to songs endpoint to view the songs
http://127.0.0.1:8000/api/v1/songs/
In the next post, we will add authentication and security using jwt. Stay connected ...
PART-2
Keep loving bugs! Have a good day
PART-2
Keep loving bugs! Have a good day
Saturday, August 6, 2022
Django
Nodejs
python
vuejs
web development
The material and aggregation is excellent and telltale as comfortably.
ReplyDeletedownload showbox apk
This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine.
ReplyDeleteMusic streaming
Good resource for Django rest framework
ReplyDeleteHi,
ReplyDeleteWonderful Article, I liked the part where you mentioned the Streaming App services most of the companies who want to make an app like Netflix Clone. Most of the users are not aware of How do Streaming Platforms Make Money? We want our readers to get useful and informative knowledge.its good to bring awareness because every efforts helps.
https://foxleak.com/top-stories/why-naruto-is-loved-by-so-many-fans-all-over-the-world
ReplyDeleteDelivering Technology and Strategic Intelligence. Your go-to partner for mobile development, web development, healthcare, cybersecurity, custom software, and e-commerce solutions. https://vengix.com/
ReplyDelete