Django Basics - First step to Simple REST API

         Recently I had been watching Bucky's django series and I got this idea to make a simple API. After few attempts I made it. Its really a simple one. For the initial phase, I just used post title and first name in my database.

API, basically  a way to communicate between different parties. One party is providing its data to others. Others need to ask polite (protocal) to get data. Always API returns standard format.Like JSON. other parties can use these data for their own works.(as I understood)

        Lets get started, first we need to have django installed in your PC. Its very easy. Open your Terminal (CMD) and type,

             sudo easy_install django 

(Here I am explaining the Linux terminal way, if you use windows. Just ignore the sudo part.)

Ok, we now have Django in our PC, create a folder somewhere you want, and go to it from terminal (cd command).

            sudo django-admin startproject your_project_name

Here you can use an appropriate name for your project. Now we have our django project installed. Django is different, because this just creates necessary files for the django not for our works. So we have to create an app (our working project).

           sudo python manage.py startapp your_app_name

First thing first, when we create an app, we must notify it to django. to do it, you have to list your app in django's installed app directory. Go to 
      Your_project->settings.py -> INSTALLED_APPS

Add your app to this array. For me, I named my app as simple_rest, so it looks like this
              'simple_rest.apps.SimpleRestConfig'

                                             Figure 1 : settings.py file

For now, just ignore rest_framwork.Did you notice another thing, There is a thing called SimpleRestConfig  what is that name? Actually it a name django gives when we create our project. You can find your Project Config name this files, class name
 Your app folder -> apps.py

Now we need an admin for the system. This helps us to insert data to our model easily. But first we have to migrate the database files. What, we didn't create any model yet, so how we migrate?
Actually Django creates some default templates. Like users, authentication. So now you just have to migrate them and then create an admin for the system.

          sudo python manage.py makemigrations    # make changes 

         sudo python manage.py migrate                 #migrate files

         sudo python manage.py createsuperuser

You will be asked for a username, email and password, provide them properly. Then you can access to the following url

          localhost:8000/admin/

Oh, an errror. Page is not found. I forgot to start django server. So lets start it.

         sudo python manage.py runserver

Ok, now we have fundamental things, lets build our project.
First we need data, lets create our Model for holding data. just go to your_app->models.py

For this example, I am making a simple one.

           class SimpleAPI(models.Model):
          post_title = models.CharField(max_length=25)
          first_name = models.CharField(max_length=25)

          def __self__(self):            # this is like a constructor for the model
         return self.post_title  # always return post titles (if there are data)

We created our model, now we need to migrate it to our database. By default Django uses SQLite database. 

So we just have to execute these two commands.

         sudo python manage.py makemigrations    

         sudo python manage.py migrate 

Now we have a table for our project. There are few ways to insert data to the database. Using SQLite browser, or django's shell. But we created an admin. Django admin can add,edit,delete records. All necessary views are provided too (pretty easy).
In Your app folder->admin.py 
at the top, put this
     from .models import SimpleAPI   #use your model name
Then


          admin.site.register(SimpleAPI) #you model name

Done, now go to localhost/admin/

You can see your model there. And its just simply add some records. Now we have data in our database, In the next post, lets see how to return them as an API (like a boss :p_)

Comments

Popular posts from this blog

Youtube-dl cheatsheet

Git mistakes - 1

Docker Development - Mistakes 1