Subscription
Free Trial
Renews at $19.99p/m What do you get with Print? ORPayment Processing...
Completed Shipping AddressBilling is same as shipping information
View table of contents Preview Book Django 5 By Example The request/response cycleLet’s review the request/response cycle of Django with the application we built. The following schema shows a simplified example of how Django processes HTTP requests and generates HTTP responses:
Figure 1.19: The Django request/response cycleLet’s review the Django request/response process:
https://domain.com/blog/33/
. The web server receives the HTTP request and passes it over to Django./blog/<id>/
matches the path /blog/33/
.HttpRequest
class and the keyword or positional arguments. The view uses the models to retrieve information from...In this chapter we have introduced a variety of Django management commands. You need to get familiar with them, as they will be used often throughout the book. Let’s revisit the commands we have covered in this chapter.
To create the file structure for a new Django project named mysite
we have used the following command:
django-admin startproject mysite
To create the file structure for a new Django application named blog
:
python manage.py startapp blog
To apply all database migrations:
To create migrations for the models of the blog
application:
python manage.py makemigrations blog
To view the SQL statements that will be executed with the first migration of the blog
application:
python manage.py sqlmigrate blog 0001
To run the Django development server:
python manage.py runserver
To run the development server specifying host/port and settings file:
python manage.py runserver 127.0.0.1:8001 --settings...
Summary
In this chapter, you learned the basics of the Django web framework by creating a simple blog application. You designed the data models and applied migrations to the database. You also created the views, templates, and URLs for your blog.
In the next chapter, you will learn how to create canonical URLs for models and how to build SEO-friendly URLs for blog posts. You will also learn how to implement object pagination and how to build class-based views. You will also implement Django forms to let your users recommend posts by email and comment on posts.
Adding paginationWhen you start adding content to your blog, you can easily store tens or hundreds of posts in your database. Instead of displaying all the posts on a single page, you may want to split the list of posts across several pages and include navigation links to the different pages. This functionality is called pagination, and you can find it in almost every web application that displays long lists of items.
For example, Google uses pagination to divide search results across multiple pages. Figure 2.3 shows Google’s pagination links for search result pages:
Figure 2.3: Google pagination links for search result pages
Django has a built-in pagination class that allows you to manage paginated data easily. You can define the number of objects you want to be returned per page and you can retrieve the posts that correspond to the page requested by the user.
Adding pagination to the post list viewWe will add pagination to the list of posts so that...
Building class-based viewsWe have built the blog application using function-based views. Function-based views are simple and powerful, but Django also allows you to build views using classes.
Class-based views are an alternative way to implement views as Python objects instead of functions. Since a view is a function that takes a web request and returns a web response, you can also define your views as class methods. Django provides base view classes that you can use to implement your own views. All of them inherit from the View
class, which handles HTTP method dispatching and other common functionalities.
Class-based views offer some advantages over function-based views that are useful for specific use cases. Class-based views allow you to:
GET
, POST
, or PUT
, in separate methods, instead of using conditional branchingWe will allow users to share blog posts with others by sending post recommendations via email. You will learn how to create forms in Django, handle data submission, and send emails with Django, enhancing your blog with a personal touch.
Take a minute to think about how you could use views, URLs, and templates to create this functionality using what you learned in the preceding chapter.
To allow users to share posts via email, we will need to:
views.py
file that handles the posted data and sends the emailurls.py
file of the blog applicationLet’s start by building the form to share posts. Django has a built-in forms framework that allows you to create...
Creating a comment systemWe will continue extending our blog application with a comment system that will allow users to comment on posts. To build the comment system, we will need the following:
Let’s start by building a model to store user comments on posts.
Open the models.py
file of your blog
application and add the following code:
class Comment(models.Model):
post = models.ForeignKey(
Post,
on_delete=models.CASCADE,
related_name='comments'
)
name = models.CharField(max_length=80)
email = models.EmailField()
body = models.TextField()
...
Summary
In this chapter, you learned how to define canonical URLs for models. You created SEO-friendly URLs for blog posts, and you implemented object pagination for your post list. You also learned how to work with Django forms and model forms. You created a system to recommend posts by email and created a comment system for your blog.
In the next chapter, you will create a tagging system for the blog. You will learn how to build complex QuerySets to retrieve objects by similarity. You will learn how to create custom template tags and filters. You will also build a custom sitemap and feed for your blog posts and implement full-text search functionality for your posts.
Download CodeIf you want to learn Django by doing, this book is for you. This 2025 EDITION, fully updated to Django 5.2 LTS, is the fifth edition of the best-selling Django By Example franchise that helps you build real-world web apps. This book will walk you through planning and creation, solving common problems, and implementing best practices using a step-by-step approach. You’ll cover a wide range of web application development topics through four different projects: a blog application, a social website, an e-commerce application, and an e-learning platform. Pick up what’s new in Django 5 as you build end-to-end Python web apps, follow detailed project plans, and understand the hows and whys of Django. This is a practical and approachable book that will have you creating web apps quickly.
This book is for readers with basic Python programming knowledge and programmers transitioning from other web frameworks who wish to learn Django by doing. If you already use Django, or have in the past, and want to learn best practices and integrate other technologies to scale your applications, then this book is for you too. This book will help you master the most relevant areas of the framework by building practical projects from scratch. Some previous knowledge of HTML and JavaScript is assumed.
Economy delivery 10 - 13 business days
Free $6.95
Premium delivery 6 - 9 business days
$21.95
(Includes tracking information)Publication date : Apr 30, 2024
Length: 822 pages
Edition : 5th
Language : English
ISBN-13 : 9781805125457
What do you get with Print? ORPayment Processing...
Completed Shipping AddressBilling is same as shipping information
Estimated delivery fee Deliver to United StatesEconomy delivery 10 - 13 business days
Free $6.95
Premium delivery 6 - 9 business days
$21.95
(Includes tracking information)Publication date : Apr 30, 2024
Length: 822 pages
Edition : 5th
Language : English
ISBN-13 : 9781805125457
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4