DEV Community

Cover image for Django with Rest Tutorial -2
Siddharth
Siddharth

Posted on

Django with Rest Tutorial -2

Hey everyone this is the 2nd tutorial of Django and in this we will be creating database of Hogwarts and get Harry and his friends registered in it. So lets start

Quick Recap

Created a project named harrypotter/Hogwarts and created an app for grffyindor for it.And added all the apps in INSTALLED apps in settings.py
Commands used until now

django-admin startproject Hogwarts
python manage.py startapp grffyindor
Enter fullscreen mode Exit fullscreen mode

So now lets move forward from here.

Folder Structure:

Image description

So here is what the folder structure might look like in your machine and now coming to all files that are mentioned in here

urls.py This file contains all the urls and function mapped to it for now it would be having admin/ url only that comes in by default we will creating our own urls soon!!

settings.py This is an important file it contains all settings of your project from connection to database to the baked authentication that djangon provides plus if you want to create custom authentication using JSON web tokens we need to edit here first here

So for Hogwarts folder these are the files that you need to know about as of now further as we move we will discuss other folders

Now to our app Grffiyndor
views.py This file mostly contains all the functions that helps us to send data to frontend from database and modify it on our needs

models.py This is a really important file here we create all the tables that we need to store our data soon we will be creating our table for storing details of the database.

tests.py This file is majorly used for testing and creating testcases for our app

Now these are majorly all the folders you need to know.

Making our First API

So create a folder named urls.py in Grffyindor once you have done that go to urls.py of Hogwarts and write the following line of code
Hogwarts urls.py

from django.contrib import admin
from django.urls import path, include
#make sure to import include
urlpatterns = [
    path("admin/", admin.site.urls),
    path("grffyindor/", include('grffyindor.urls'))
]

Enter fullscreen mode Exit fullscreen mode

So basically here when we are adding including grffyindoor/ path and writing include('grffyindor.urls') it means that all the urls starting with grffyindoor/ will be checked in grffyindor urls file for further path

Now once you have done this open urls.py of Grffyindor and write the following piece of code:

Grffyindor urls.py

from django.urls import path
from . import views
urlpatterns = [
    path('hello/', views.hello)
]

Enter fullscreen mode Exit fullscreen mode

So here we have created a path hello/ when we go to grffyindor/hello/ our hello function fires off which helps in processing the data.

After doing this open views.py file and lets write our first view to return something to frontend

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.


def hello(request):
    return HttpResponse("Welcome to Grfyindoor")

Enter fullscreen mode Exit fullscreen mode

So here we have created a function hello() which takes an object by default it should be request and its passed automatically here the request object refers to the incoming request from client we will be exploring this request soon now moving inside we are just returning a Welcome message now you might ask that why did we write HttpResponse() instead of just writing returning that string firstly we can't send a string it should be a json/xml file so that the frontend language could understand it. And a response has certain headers too now we can manually add them but its alot of work so we use HttpResponse.

Now lets fire up our server

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

and open chrome or whatever browser you have and go to the url you created i.e http://127.0.0.1:8000/grffyindor/hello/

Congrats!!! You are Welcomed to Hogwarts and you created an api all by yourself, Lets take a break and celebrate it.

Creating a Database

So lets start with our database
Django uses a ORM for creating a database and communicating with it you can think it of as a middle man.And each table is a class in django and you can communicate with it using an object as simple as that. So lets open models.py file in grfyindoor and lets create a table Student.With some simple propertys like student name,age and gender and we will add more once we move forward

from django.db import models
from .constants import GENDER_CHOICES

class Student(models.Model):
    Name = models.TextField()
    Age = models.IntegerField()
    Gender = models.CharField(max_length=2, choices=GENDER_CHOICES)
Enter fullscreen mode Exit fullscreen mode

So here we have created 3 field Name that is text ,Age that is Integer and Gender which is choice field we have created the choices in a constants file in the same directory this is done to make your code much cleaner and readable and the max_length basically specify its length that will be stored in DB.The constants.py file is defined below

GENDER_CHOICES = (
    ("M", "MALE"),
    ("F", "FEMALE"),
    ("O", "OTHER")
)
Enter fullscreen mode Exit fullscreen mode

Once you have made these changes run the following commands

python manage.py makemigrations
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

So the first command basically tells the database management system to create the database and finally we migrate i.e save it once we do that you can go and check the migrations folder with 0001_intial.py file it basically has our first migration and as we do more changes more files will be created and we can also go back to a certain migration if there was mistake just like github we can go back to an older version.

So this is all for this tutorial. Will be talking more about models and database in the next part of the tutorial
Until then

Top comments (0)