Creating a Django Project

Django is a Python framework that makes it easier to create web sites using Python.

Django takes care of the difficult stuff so that you can concentrate on building your web applications.

Django emphasizes reusability of components, also referred to as DRY (Don't Repeat Yourself), and comes with ready-to-use features like login system, database connection and CRUD operations (Create Read Update Delete).

Django Requires Python

To check if your system has Python installed, run this command in the command prompt:


python3 --version

To install Django, you must use a package manager like PIP.To check if your system has PIP installed, run this command in the command prompt:


pip --version

To create a virtual environment, type the following command:


python3 -m venv venv

 

This will set up a virtual environment, and create a folder named "venv" with subfolders .

Then you have to activate the environment, by typing this command:


source venv/bin/activate

Now, to install django, type the comand.


pip install django==4.1.3

or just pip install django, to install the latest version

Now to create a project, type this command.


django-admin startproject my_blog

Where my_blog is the name of the Django project

Navigate to the /my_blog folder and execute this command


python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py runserver 

Which will produce this result:


Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 27, 2022 - 13:03:14
Django version 4.1.2, using settings 'my_tennis_club.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK. 

Open a new browser window and type 127.0.0.1:8000 in the address bar.

You should get the congratulations page of Django

To create an app, start by navigating to the selected location where you want to store the app, in my case the my_blog folder, and run the command below.


python3 manage.py startapp blog

This will create a folder name "blog" in project folder.

Now in the settings.py file of the "my_blog" project, add "blog" to INSTALLED_APPS.


INSTALLED_APPS = [
      'blog',
]

In the urls.py file of the "my_blog" project add the following lines.


from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('', include('blog.urls')),
    path('admin/', admin.site.urls),
]

Navigate to the /my_blog folder, inside it you will get a "blog" folder, in that folder in the views.py file, add the following lines


from django.shortcuts import render
from django.http import HttpResponse

def index(request):
      return  HttpResponse("Hello World")

Navigate to the /my_blog folder, inside it you will get a "blog" folder, in that folder in the urls.py file add the following code


from django.urls import path
from . import views

urlpatterns = [
	path('', views.index, name="index"),
]

Now run python3 manage.py runserver to see the result

Latest Blog