Introduction to Django
Django is a high-level Python web framework that promotes rapid development and clean, pragmatic design. Initially released in 2005, Django has garnered a large community of developers due to its effectiveness in speeding up web development while providing a sturdy foundation.
Built by developers, for developers, Django alleviates the hassle of web development, allowing you to focus on writing your app without reinventing the wheel. Key features include a robust ORM, automatic admin interface, form handling, and security tools.
Setting Up Your Development Environment
Prerequisites
To get started with Django, ensure you have Python installed on your system. As of this writing, Django supports Python 3.8 and above. You can download it from the official Python website.
Installing Django
Once Python is installed, you can install Django using pip, Python’s package installer. Run the following command:
pip install django
Verify the installation by executing:
django-admin --version
Creating Your First Django Project
Starting a Project
With Django installed, you’re ready to create your first project. Navigate to the directory where you want to store your project and run:
django-admin startproject mysite
This command generates a new directory called mysite containing the core files necessary for a Django project.
Understanding Project Structure
The directory structure will look like this:
manage.py: A command-line utility that lets you interact with your Django project.mysite/: The project’s package.__init__.py: An empty file that tells Python this directory should be considered a package.settings.py: Configuration for this Django project.urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site.wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.
Running the Development Server
To ensure everything is set up correctly, use Django’s lightweight development web server. Navigate to the outer mysite directory and run:
python manage.py runserver
In your browser, navigate to http://127.0.0.1:8000/. You should see Django’s “It worked!” confirmation page, indicating that your project is active.
Creating Your First App
In Django, an app is a web application that does something, like a blog or a database of public records. An app is what goes in a project, and a project can have multiple apps. To create an app, navigate to the desired project directory where manage.py is located and execute:
python manage.py startapp myapp
Django will create a directory structure for the app, similar to the one for a project.
Defining Models
What are Models?
Models are the single, definitive source of information about your data. They contain essential fields and behaviors of the data you’re storing.
Creating Models
For example, a blog application might have a model to represent a blog post. Open the file myapp/models.py and add:
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
published_date = models.DateTimeField()
Working with the Admin Interface
Django’s admin interface is a powerful tool for managing your site’s data. To start using it, create a superuser account:
python manage.py createsuperuser
Follow the prompts to set up your account. Then, register your models with the admin in myapp/admin.py:
from django.contrib import admin
from .models import Post
admin.site.register(Post)
Access the admin interface by navigating to http://127.0.0.1:8000/admin/ and logging in.
Creating Views and Templates
Working with Views
Views are Python functions or classes that take a web request and return a web response. Open myapp/views.py and define a simple view:
from django.shortcuts import render
from .models import Post
def home(request):
posts = Post.objects.all()
return render(request, 'myapp/home.html', {'posts': posts})
Setting Up Templates
Templates control how data is displayed. Create a directory templates/myapp and within it, create home.html with the following content:
Home
{% for post in posts %}
{{ post.title }}
{{ post.content }}
Published on {{ post.published_date }}
{% endfor %}


0 Comments