Introduction to Web application development with Django

Начать

Django 6 course for beginners: build a Notes app with models and migrations, the ORM, templates, ModelForm and CSRF, the admin and generic class-based views, served on the sandbox's public URL, with every error provoked and explained.

Совместимая лаборатория

Python Web Development Sandbox

Build Python web applications and REST APIs online in the Bytestark Web Development Sandbox. Use Django, Flask, FastAPI, Bootstrap, and SQLite directly in your browser without installation....

10,0 (1)

Главы

11
Глава 1

What Django Is and How a Web Framework Thinks

This chapter explains what Django is and how an HTTP request travels from the browser to the response: middleware, URLconf, view, model, template. It introduces the Model–View–Template architecture, the difference between a project and an application, and the role of the development server in the sandbox. By the end you know what each part of the notes application we build in the rest of the course does, and why it exists.

Глава 2

First Project: startproject, the Development Server and the Sandbox URL

In this chapter you generate the Django project with django-admin startproject, learn the role of every generated file and of the key settings in settings.py, and start the development server on 0.0.0.0:9000, the only port wired to the sandbox's public URL. The first request from the browser fails on purpose with 400 DisallowedHost; you see why Django checks the Host header and why ALLOWED_HOSTS = ["*"] is right in the sandbox and wrong in production. By the end you can read the server's log line and the response headers with curl.

Глава 3

First Application: Routes and Views

In this chapter you create the notes application, write the first function views and connect them to addresses through a URLconf of their own, included in the project's. You see what Django checks before it sends a response by provoking the "didn't return an HttpResponse object" error, how the int converter extracts a number from the path and hands it to the view, and how to read the 404 page that lists the routes tried. By the end you also understand the 301 redirect for addresses without a trailing slash.

Глава 4

Templates: From HttpResponse to Template Files

In this chapter you move the notes application's home page into a Django template and learn how the framework finds it: you provoke the TemplateDoesNotExist error, read the Template-loader postmortem and discover why the application has to be declared in INSTALLED_APPS. Then you send a list from the view through the context, display it with for, if, empty and the length and pluralize filters, watch automatic escaping neutralise an injected script, and split the structure of the pages into a base template with blocks.

Глава 5

Models and Migrations: The Notes Table

In this chapter you describe the notes table as a Django model, with a TextField, a DateTimeField filled in automatically at creation and a BooleanField with a default, and learn what each decision means. You generate the migration with makemigrations, read the generated file and its SQL with sqlmigrate, apply all the project's migrations with migrate and check the notes_note table directly in SQLite. By the end you know why adding a __str__ method produces no migration while adding a field does.

Глава 6

The ORM in the Shell: Creating, Finding and Changing Notes

In this chapter you use the Django ORM in the project's shell: you create notes with save() and create(), filter them with lookups such as contains and icontains, look one up with get() and see the DoesNotExist exception, read the SQL of a lazy QuerySet and reproduce the classic bug of the unsaved change. Then you move the queries into views, with order_by and get_object_or_404, so the home page shows real notes and a note that does not exist gets a clean 404.

Глава 7

The Admin Interface

In this chapter you switch on the Django admin for the notes application: you create a superuser with createsuperuser and watch the password validators refuse a weak password, understand the 302 redirect to the login page and the session row behind it, register the Note model and add a note from the interface. Then you configure the list with a ModelAdmin class, using list_display, list_filter, search_fields and ordering, and discover that the admin's filters are ORM lookups in the page's address.

Глава 8

Forms: ModelForm, CSRF and Validation

In this chapter you build the page for adding notes with a ModelForm and go through the full cycle of a web form: display on GET, submission on POST, is_valid, save and redirect. You get a 403 Forbidden from the CSRF protection on purpose, see its two different reasons ("CSRF cookie not set" and "CSRF token missing") and fix them with csrf_token, read request.POST as a QueryDict in the server log, go around the browser's validation with curl to see the errorlist, and learn why the response is 200. At the end you follow is_valid, errors and cleaned_data in the shell.

Глава 9

Class-Based Views: Detail, Edit, Delete and Archive

In this chapter you rewrite the notes application with generic class-based views and add editing, deleting and an archive of completed notes: ListView with context_object_name, ordering and get_context_data, a subclass with get_queryset, DetailView, CreateView, UpdateView and DeleteView with reverse_lazy. You provoke the typical errors and explain them: the template derived from the naming convention, the list that renders empty because of the context name, and the circular import produced by reverse() at class level. At the end you watch dispatch() answer 405.

Глава 10

Course Summary

This chapter recaps the course along the journey of a request through Django, now with the file, the mechanism and the matching error for each link: middleware, URLconf, view, model, template, response. A table gathers every error provoked on purpose in the course, with its cause and chapter, followed by the design decisions you can now justify and the subjects deliberately left outside the course.

Тест 11

Django Basics

The final test checks the mechanisms from the course, not memorised syntax: why the server has to be started on 0.0.0.0:9000, what ALLOWED_HOSTS checks, the difference between makemigrations and migrate, why a change without save() does not reach the database, how the CSRF error appears and how it is fixed, what is_valid() does and why a ListView can render an empty list. Ten questions with four options and one correct answer each.

Комментарии

Комментариев пока нет. Будьте первым, кто поделится своим мнением.
Средний
9,0 (1)

Автор

Software engineer and the creator of the Bytestark learning platform