Django is a powerful, high-level web framework for the Python programming language. It encourages rapid development and clean, pragmatic design. If you’re interested in learning how to build web applications using Django, this guide will help you create a simple web app from scratch. By the end of this article, you’ll have a basic understanding of how Django works and the knowledge to build more complex applications.
Prerequisites
Before diving into Django, make sure you have a basic understanding of Python. Additionally, you’ll need to have Python installed on your machine. You can download Python from the official Python website if it’s not already installed.
Setting Up Your Environment
To get started with Django, you’ll need to set up your development environment. This involves installing Django and creating a virtual environment.
Installing Django
pip install django
After installing Django, confirm the installation by typing the following command:
django-admin --version
Creating a Virtual Environment
A virtual environment is a self-contained directory that contains a Python installation for a particular version of Python, plus a number of additional packages that you can install. Creating a virtual environment is considered best practice as it keeps dependencies required by different projects in separate places.
python -m venv myenv
source myenv/bin/activate # On Unix/Mac
myenv\Scripts\activate # On Windows
Starting a New Django Project
With Django installed and your virtual environment activated, you can start a new project. In Django, a project is a collection of configuration and apps for a particular website. Let’s create a new project called “myfirstproject”.
django-admin startproject myfirstproject
This command creates a “myfirstproject” directory with the following structure:
myfirstproject/
manage.py
myfirstproject/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
The manage.py file is a command-line utility that lets you interact with your project. The second “myfirstproject” directory is the actual Python package for your project. It’s where settings, URLs, and WSGI configurations are stored.
Running the Development Server
Django comes with a lightweight web server for development and testing purposes. To start it, navigate to the “myfirstproject” directory containing the manage.py file and run:
python manage.py runserver
You should see output indicating the server is running, typically on http://127.0.0.1:8000/. Open this URL in your web browser, and you’ll see the Django welcome page.
Creating a New App
A Django app is a web application that does something – e.g., a blog, a database of public records, or a simple poll app. Create a new app named “hello” inside your project:
python manage.py startapp hello
This command creates a new directory called “hello” with the following basic structure:
hello/
migrations/
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
Writing Your First View
In Django, a view is a Python function that receives a web request and returns a web response. First, edit the views.py file in your “hello” app to include a simple view:
from django.http import HttpResponse
def hello_view(request):
return HttpResponse("Hello, world!")
Configuring URLs
To make the view accessible, you’ll need to map a URL to it. Create a new file called urls.py in the “hello” app directory and include the following code:
from django.urls import path
from . import views
urlpatterns = [
path('hello/', views.hello_view, name='hello'),
]
Next, include this URL pattern in the main project’s urls.py file:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include('hello.urls')),
]
Running Your Web App
With the server running, visit http://127.0.0.1:8000/hello/ in your web browser. You should see “Hello, world!” displayed on the page.
Conclusion
Congratulations! You’ve successfully created your first Django web app. This guide covered the basics of setting up a Django project, creating a simple app, and running a development server. From here, you can explore Django’s powerful features, such as its ORM for database operations, user authentication, admin interface, and more. Django’s comprehensive documentation is an excellent resource for deepening your knowledge and building more complex applications. Happy coding!
0 Comments