❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Getting Started with Django: Creating Your First Project and Application

By: Sakthivel
12 November 2024 at 14:17

Django is a powerful and versatile web framework that helps you build web applications efficiently. Here’s how you can set up a Django project and create an application within it.

Step 1: Install Django

First, you’ll need to install Django on your machine. Open your terminal or command prompt and run:

pip install django

Step 2: Check Django Version

To confirm Django is installed, you can check the version by running:

python3 -m django --version

Step 3: Set Up Project Directory

Organize your workspace by creating a folder where your Django project will reside. In the terminal, run:

mkdir django_project
cd django_project

Step 4: Create a Django Project

Now, create a Django project within this folder. Use the following command:

django-admin startproject mysite

This will create a new Django project named mysite.

Project Structure Overview

Once the project is created, you’ll notice a few files and folders within the mysite directory. Here’s a quick overview:

  • manage.py: A command-line utility that lets you interact with this Django project in various ways (starting the server, creating applications, running migrations, etc.).
  • __init__.py: An empty file that tells Python to treat the directory as a Python package.

Step 5: Run the Development Server

Now that the project is set up, you can test it by running Django’s development server:

python3 manage.py runserver

Visit http://127.0.0.1:8000/ in your browser, and you should see the Django welcome page, confirming your project is working.

Step 6: Create an Application

In Django, projects can contain multiple applications, each serving a specific function. To create an application, use the command:

python3 manage.py startapp firstapplication

This will create a folder named firstapplication inside your project directory, which will contain files essential for defining the app’s models, views, templates, and more.

With this setup, you’re ready to start building features in Django by defining models, views, templates, and URLs. This foundation will help you build scalable and structured web applications efficiently.


TASKS:

1. Create a Django Application to display Hello World Message as response.

2. Create One Django Application with multiple views.

3. Create a Django Application to display Current Date and Time.

Open settings.py inside the project folder and add a application name(firstapplication) to INSTALLED APPS List[]

Next open the views.py inside the application folder import httpresponse and create a function.

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

# Create your views here.
def display(request):
    views_content='<h2>HELLO WORLD!<H2>'
    return HttpResponse(views_content)

def display1(request):
    views_content='<h2>WELCOME TO DJANGO<H2>'
    return HttpResponse(views_content)

def display2(request):
    views_content='<h2>WELCOME TO MY APPLICATION<H2>'
    return HttpResponse(views_content)

import datetime

# Create your views here.

def time_info_view(request):
    time = datetime.datetime.now()
    output = '<h1> Currently the time is ' + str(time) + '</h1>'
    return HttpResponse(output)

next open the urls.py inside the project folder

from firstapplication(application) import views

create a urls of the page in urlpatterns list[]

from django.contrib import admin
from django.urls import path
from firstapplication import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('welcome/',views.display),
    path('second/',views.display1),
    path('third/',views.display2),
    path('datetime/',views.time_info_view)
]

Now open the local host 127.0.0.1:8000 you will see the below page

it contains 5 pages admin(default),welcome,second,third,datetime these are we created ones.

If you change the path 127.0.0.1:8000 to 127.0.0.1:8000/welcome/ you will see below page

If you change the path 127.0.0.1:8000 to 127.0.0.1:8000/datetime/ you will see below page

If you change the path 127.0.0.1:8000 to 127.0.0.1:8000/second/ you will see below page

If you change the path 127.0.0.1:8000 to 127.0.0.1:8000/third/ you will see below page

These are the essential steps for creating a Django project as a beginner, helping you understand the basic setup and flow of a Django application.

❌
❌