閉じるボタン Login as
Member / Promoter

[KIT] How to build an application ( Django)

All images are uploaded from Pixabay.com

Python is one of the most popular languages nowadays as its popularity keeps increasing days by days. Django is a python-based open source web framework which follows the MVT ( Model View Template) patterns. In this article, we will show you how to build a polls application using Django web framework in order to solidarize your understanding towards this particular framework. ( Everything in this article is written using my knowledge that I have regarding Django framework so if there is something which is inappropriate or not necessary, please let me know )

Firstly, go to your favorite integrated development environment ( IDE ) and create a new project. I am using conda virtual environment anyway and if you’re not familiar with it, please have a look at it to ensure that u will understand everything throughout the walkthrough in this article.

Activate your conda virtual environment by running this command: 

conda activate myenv

Then everything is ready. Run this command in your terminal in order to start our django project.

django-admin startproject mysite

You should see a mysite directory in your directory tree structure now so go into that directory and run a command to start our application.

python manage.py startapp polls

Now you will also see a polls directory in our tree structure. That directory will house our polls application.

Well let’s try running our server and see whether it is working or not by using this command:

python manage.py runserver

Now, go into the polls directory and create a file called urls.py. urls.py is used to store our polls application urls. Put this code in urls.py:

polls/urls.py

from django.urls import path

from . import views

urlpatterns = [

    path(‘polls’, views.index, name=’index’),

]

The next step is to point the root URLconf of our Django application at the polls.urls module

from django.contrib import admin

from django.urls import include, path

urlpatterns = [

    path(”, include(‘polls.urls’)),

    path(‘admin/’, admin.site.urls),

]

Now, let’s create a model for our application, paste this code to our models.py in our polls, directory

from django.db import models

class Question(models.Model):

    question_text = models.CharField(max_length=200)

class Answer(models.Model):

    question = models.ForeignKey(Question, on_delete=models.CASCADE)

    choice_text = models.CharField(max_length=200)

    votes = models.IntegerField(default=0)

After all of these now let’s run these 2 commands in order to save our changes into the database.

Python manage.py makemigrations

Python manage.py migrate

Okay before moving further, let’s have a quick recap of what we were doing. Firstly, we create a conda virtual environment for our Django application. Virtual environment is necessary for our project because package separation for different projects is necessary in order to avoid package chaos and virtual environment did just that. Then we run a command to create our project structure and our application structure. After that we create a urls.py file in our poll directory so that we can have a place to store our poll application url as there will be many applications in a single Django project so storing it at root url config is not advised at all. Next we create a model which is just another way of saying creating a table to store data in our database, it’s just that Django got ORM which is very convenient.

This is what my current knowledge can offer but we’ll definitely continue where we’re leave off once I finished my courses!

Related posts

  1. [KIT] SakuraFe Payment Service

  2. [KIT] Top 3 cafe shops for self-learning if you have no internet connection at home.

  3. [KIT] Top 3 Destinations You Should Not Miss When You Visit Battambang Province

  4. [KIT] 3 Basic Ways For Long-term Remembering

  5. [KIT] Top 5 Netflix series

  6. [KIT] Top 9 Programming Language to learn in 2021