Django is a high-level web framework that is written in Python. It is a powerful tool for building web applications quickly and easily. If you are new to web development with Django, this tutorial will guide you through the process of creating a new project and app.
Prerequisites Before we start, make sure you have the following installed on your computer:
pip (Python Package Manager)
Installation Once you have Python and pip installed, you can install Django using pip. Open up your terminal or command prompt and type the following command:
pip install django
This will install the latest version of Django on your computer.
Creating a Project Now that Django is installed, let's create a new project. Open up your terminal or command prompt and navigate to the directory where you want to create your project. Once you are in the desired directory, type the following command:
django-admin startproject myproject
This will create a new directory called "myproject" with the following structure:
myproject/ manage.py myproject/ __init__.py settings.py urls.py asgi.py wsgi.py
manage.py script is used to manage your Django project and the
myproject directory contains the settings and configurations for your project.
Creating an App Now that we have a project, let's create an app. An app is a modular unit of functionality that can be reused across projects. To create an app, navigate to your project directory in the terminal or command prompt and type the following command:
python manage.py startapp myapp
This will create a new directory called "myapp" with the following structure:
myapp/ __init__.py admin.py apps.py models.py tests.py views.py
views.py file is where you will define the views for your app.
Defining a View Let's define a simple view for our app. Open up the
views.py file in your editor and add the following code:
from django.http import HttpResponse def hello(request): return HttpResponse("Hello, world!")
This code defines a view called
hello that returns a simple HTTP response.
Mapping a URL Now that we have a view, we need to map a URL to it. Open up the
urls.py file in your app directory and add the following code:
from django.urls import path from . import views urlpatterns = [ path('hello/', views.hello, name='hello'), ]
This code maps the URL
/hello/ to the
hello view we defined earlier.
Running the Server We're almost done! Let's start up the development server and see our app in action. Navigate to your project directory in the terminal or command prompt and type the following command:
python manage.py runserver
This will start the development server. Open up your web browser and navigate to
http://localhost:8000/hello/. You should see the message "Hello, world!" displayed in your browser.
Congratulations! You have successfully created a new Django project and app, defined a view, mapped a URL, and run the development server. This is just the beginning of what you can do with Django. With its powerful features and flexible architecture, Django is a great tool for building web applications of any size and complexity.