top of page

A Beginner's Guide to Installing Django

Updated: Apr 27, 2023

INTRODUCTION

Django is a high-level open-source web framework for building web applications in Python.


Django provides a robust set of tools and features for building web applications, including URL routing, object-relational mapping (ORM), templates, forms, authentication, and more. It also has a built-in administrative interface that makes it easy to manage application data and content.


Django is widely used for developing a variety of web applications, from simple websites to complex web applications used by large organizations. It is known for its scalability, security, and flexibility, making it a popular choice among developers.


In this blog, you will learn:

  1. To install Django.

  2. To create virtual environment.


STEP 1

To start installing Django, we need to create an empty directory where we can install the framework. For this, we will create a new directory with the name "web_development". This directory will serve as the main directory for different Django projects.

STEP 2

We will open this location of "web_development" in command prompt.



STEP 3

We need to create a directory that will act as the virtual environment for our Django project. Let's call this directory "django_march_23". To create it, we can simply open the command prompt and type "mkdir django_march_23". Then, we can view the contents of the current directory using the command "dir" and make sure that "django_march_23" has been successfully created. To enter the newly created directory, we can use the command "cd django_march_23". This will allow us to work on our Django project within this virtual environment.


STEP 4

To create the virtual environment for our Django project, we will use the "virtualenv" command. However, if we receive an error message saying that the command "virtualenv" is not recognized, it means that the virtualenv package is not installed on our system. To fix this, we can simply install virtualenv using the "pip" command. We can type "pip install virtualenv" in the command prompt and wait for the installation to complete. Once installed, we can use the "virtualenv -p python ." command to create our virtual environment within the "django_march_23" directory. This will set up a separate Python environment for our project, allowing us to install packages and libraries specific to our needs without affecting the global Python installation on our system.


STEP 5

Now that we have created the virtual environment, we need to activate it. To do this, we will use the command "Scripts\activate" in the command prompt. This will activate the virtual environment and set it as the default Python environment for our project. Once the virtual environment is active, we can install Django using the "pip install django" command. This will download and install the latest version of Django within our virtual environment, ensuring that it is isolated from the global Python installation on our system.

STEP 6

Now that we have installed Django, we need to create the source directory for our project. Let's call this directory "src". To create it, we can use the command prompt and type "mkdir src". Then, we can navigate to the newly created directory by typing "cd src". Once we're inside the "src" directory, we can create our Django project using the "django-admin startproject django_16_march ." command. This will create a new directory named "django_16_march" inside the "src" directory, which will contain all the necessary files and directories for our project. To make sure that the project has been created successfully, we can use the "dir" command to view the contents of the "src" directory. Among the files and directories, we should see a file named "manage.py". This file is essential for running our Django project on a server.

STEP 7

To view our Django project, we need to run the local server. To do this, we can type "python manage.py runserver" in the command prompt while inside the project directory. This will start the server and provide us with a URL where we can view our project. In this case, the URL will be "http://127.0.0.1:8000/". We can simply copy and paste this URL into our web browser to view our Django project.

STEP 8

To view our Django project, we can simply copy and paste the URL that was provided after running the "python manage.py runserver" command into our web browser. This will open up our project in the browser, allowing us to view and interact with it.

STEP 9

Once we have done that, we will change the media and static files directory in the settings.py


NOTE: Most probably the variables are already declared in the file, so we have to change only the values of those variables.


To do the we will add this script to the file:


STATIC_URL = '/static/'

STATICFILES_DIRS = [
    BASE_DIR / 'static_files'
]


STATIC_ROOT = BASE_DIR / "static"


MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

This code is a portion of the settings.py file in a Django project. It defines various settings related to static files, media files, and the default primary key field type.


Static files are files that are used to provide CSS, JavaScript, images, and other static assets for your web application. They are served directly by the web server without any processing by Django. The STATIC_URL setting defines the URL prefix for static files. In this case, it is set to '/static/', meaning that any URL that starts with '/static/' will be interpreted as a request for a static file.


The STATICFILES_DIRS setting defines the directories where Django should look for static files. In this case, it is set to [BASE_DIR / 'static_files'], which means that Django will look for static files in the static_files directory located in the project's base directory.


The STATIC_ROOT setting defines the directory where Django should collect all the static files for deployment. In this case, it is set to BASE_DIR / "static", meaning that when you run the collectstatic command, Django will collect all the static files in the project's static directory.


The MEDIA_URL setting defines the URL prefix for media files. Media files are user-uploaded files such as images, videos, and audio files. In this case, it is set to '/media/', meaning that any URL that starts with '/media/' will be interpreted as a request for a media file.


The MEDIA_ROOT setting defines the directory where Django should store uploaded media files. In this case, it is set to BASE_DIR / 'media', meaning that uploaded media files will be stored in the media directory located in the project's base directory.


The DEFAULT_AUTO_FIELD setting specifies the default type of primary key field that Django should use for new models. In this case, it is set to 'django.db.models.BigAutoField', meaning that Django will use a 64-bit integer primary key field for new models.


Now, we will create a directory name 'static_files' in the directory that contains app directory.

In our case, it is directory name src.

STEP 10

After doing this, we will run the following commands,

python manage.py makemigrations
python manage.py migrate

In Django, makemigrations and migrate are two commands that are used to manage database schema changes.


makemigrations is a command that creates new database migration files based on the changes you have made to your Django models. When you make changes to your models, such as adding new fields or changing the data type of existing fields, you can use makemigrations to generate a new migration file that describes the changes you have made.


Once you have created a migration file using makemigrations, you can use the migrate command to apply the changes to your database schema. The migrate command takes the migration files generated by makemigrations and applies them to the database, making the necessary changes to the database schema.


In summary, makemigrations is used to create new migration files that describe the changes you have made to your models, while migrate is used to apply these changes to the database schema.


Together, makemigrations and migrate provide a powerful toolset for managing database schema changes in Django, allowing you to easily add, modify, and delete fields in your database schema as your application evolves over time.


STEP 11

Now we have to make the administration credentials using the command python manage.py createsuperuser


After doing this, you can visit the URL http://127.0.0.1:8000/admin/ to make sure that the account is created.


Now, we want to change the front page of our site. To do this, we will create a directory named templates in the src directory. This new directory will contain the HTML files to display. We will create a file name home.html, and we will add the following markup-language in it,

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <h1>Hello World!</h1>
</body>
</html>

It will look like this,


STEP 12

After doing this, we will add the following information to the files present in the directory django_16_march.

from django.contrib import admin
from django.urls import path
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static

from django_16_march.views import home_view

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home_view, name='home'),
    path('', include('file_upload.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL,
                              document_root=settings.MEDIA_ROOT)
from django.shortcuts import render
from django.http import HttpResponse

def home_view(request, *args, **kwargs): 
    print(args, kwargs)
    print(request.user)
    return render(request, "home.html", {})

Save the changes, and visit the URL http://127.0.0.1:8000 again, after doing this, you will see following page.


If you require any assistance with your Django projects, please do not hesitate to contact us. We have a team of experienced developers who specialize in Django and can provide you with the necessary support and expertise to ensure the success of your project. You can reach us through our website or by contacting us directly via email or phone. We look forward to hearing from you and helping you with your Django development needs.

bottom of page