Building Websites with Django

Jul 3, 2024

Building Websites with Django 🌐

Introduction

  • Course focuses on building awesome websites with Django from the ground up.
  • Starts as absolute beginners: intro to Django, basic web server setup, rendering templates, adding databases, authentication, search functionality.
  • Second half: Features to turn the project into a full-fledged application like a discord clone for study groups.
  • Example project at studybud.dev.

Project Overview: StudyBud.dev

  • Homepage: Basic feed of different study rooms.
  • Rooms: Hosts can set up topics, people can join and see participants and chats.
  • Profile View: View host's profile, chat participants, search functionality.

Pre-requisites

  • Basic understanding of Python syntax: variables, if/else, loops, classes.
  • Basic understanding of HTML and CSS.

Setting Up Environment

Tools and Extensions

  • Text Editor: VS Code recommended.
    • Extensions: Atom One Dark Pro theme, Prettier (auto code formatter), Auto Rename Tag.

Installing Python and Django

  • Ensure Python is installed (python --version). Update if necessary.
  • Install Django globally or use a virtual environment for isolation.
  • Example: pip install virtualenv, virtualenv env, env\Scripts\activate.
  • Install Django within the virtual environment: pip install django.
  • Commands overview: python manage.py startproject, runserver, startapp.

Django Basics

Core Files Overview

  • manage.py: Execute various Django commands.
  • Database: Default SQLite.
  • Settings.py: Key configuration for apps, middleware, templates, database, static files.
  • Urls.py: URL routing configuration.
  • wsgi.py and asgi.py: Server gateway interfaces.

Creating Django Apps

  • Apps handle different parts of a project (e.g., users, posts).
  • Example App: base. Configured in settings.

URL Routing

  • Define routes in urls.py.
  • Example: Create URLs for homepage and room detail pages with corresponding views.
  • Use app-specific urls.py for better organization.

Rendering Templates

  • Templates located in a directory specified in settings.py (e.g., templates folder).
  • Use render method in views to render html templates.
  • Example: render(request, 'home.html').

Template Inheritance

  • Use base templates for layout consistency (block tags and extends keywords).
  • Example: Create a main.html with navigation and base structure, extend in other templates.
  • Example of block tag usage for content sections.

Working with Django Models

Creating Models

  • Models.py: Define database tables as classes.
  • Example: Room and Message models with relationships (ForeignKey).
  • Commands: python manage.py makemigrations, migrate.

Admin Panel

  • Register models in django admin for easy data management (admin.site.register(ModelName)).
  • Example: Register Room and Message models in admin.py.

Querying Data

  • Use Django ORM: Model.objects.all(), filter(), get(), exclude(), etc.
  • Example: Query rooms and messages, pass data to views, render in templates.

CRUD Operations

Forms and Templates

  • Use Django Model Forms or manually define forms in HTML.
  • Handle form submissions in views: POST method, validate, save data, and redirect.
  • Example: Create, read, update, delete operations for Room model.

User Authentication

  • Login & Register: Use Django's built-in user authentication system.
  • Create custom login forms and validate user credentials.
  • Use decorators like @login_required for route protection.
  • Example: Custom registration view with UserCreationForm.

Search Functionality

Implementing Basic Search

  • Create search forms in templates (GET requests with search parameters).
  • Filter data in views based on search query (e.g., icontains lookup).
  • Example: Search study rooms by name, topic.

Advanced Filtering

  • Use Django ORM and Q objects for complex queries (AND/OR conditions).

Dynamic URL Handling

  • Handle dynamic URLs for detailed views (e.g., room detail page by ID).

Static Files and Theming

Configuring Static Files

  • Specify static files location in settings.py (static and media roots).
  • Load static files in templates (CSS, JS).
  • Example: Add stylesheets and scripts to the base template.

Template Installation

  • Integrate a pre-built theme by modifying Django templates.
  • Convert static content in themes to Django template tags, loops, and variables (e.g., use {% static 'path/to/file.css' %}).

Signature Features

User Profiles

  • Allow users to upload profile pictures (image fields with Pillow library).
  • Customize user models or use one-to-one relationships for additional profile data.
  • Ensure user-uploaded content is properly handled and stored (media root).

Advanced Topics

Working with APIs

  • Utilize Django Rest Framework (DRF) to build APIs.
  • Install DRF, create serializers, views, and urls for API endpoints.
  • Example API: List rooms, get details of a specific room.

Deployment

  • Considerations for deploying Django applications (e.g., database setup, static file handling).

Conclusion

  • The course provides a comprehensive guide to building a Django application from scratch to deploying the completed project.
  • Review key concepts, practice CRUD operations, test out search functionalities, and explore DRF for API setups.