Mount Django Server

Gonzalo Galante
3 min readMay 23, 2022

In this guide i will show you how use Django and make a service with this framework.

First at all, what is DJango?

In words of the developeres

“Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.”

Basically is a framework of python that allow us to mount a server (can be used as microservice or API).

There´s others frameworks like FastApi or Flask, this are more simples but also less scalable.

For this guide I will use pipenv to create a virtual environment and install all dependencies only there and not on my entire PC, is not necessary but recommended

Step by step

1- In this step we will create a folder, initialize a virtual env and install Django

mkdir testingFolder
cd testingFolder
pipenv shell
pipenv install Django

“pipenv shell” create the environment

For all the liraries that we install we use “pipenv” insted of “pip”

2- Now is time to create the project and the App

django-admin startproject "Name_of_project"
cd "Name_of_project"
python manage.py startapp "Name_of_App"
python manage.py runserver
ctrl - c
python manage.py migrate
Migration

Here Django create settings for an instance of Django and a many configuration.

The difference between a project and a App?

Django commit: “A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.”

With manage.py we start the server for default in port 8000, and to stop use “ctrl + c”

3- Creating routes for our first function

In the urls.py file that is in the project folder we modify the code by the following

from django.urls import path, include

urlpatterns = [
path("", include("Name_of_App.url"))
]

Create “url.py” inside the app folder with this code

from django.urls import path, include
from . import views

urlpatterns = [
path("test", views.test),
]

In Views.py replace the code with this

from django.http import JsonResponse

def test(request):
return JsonResponse({"response": "test"}, status=200)

Finally to test this, start the server and in the local host go to /test

“localhost:8000/test”

In views.py we create all the functions tha we request by url. To maintain a clean code it is recommended to create .py to separate the auxiliaries functions by utilities.

This seems basic and easy but it is the base, 1% of all the options we have. but it is also enough to create many tools, in the next articles I will show you how to build a scrapper with django.

if want learn how upload these to cloud read this guide

--

--