This course assumes familiarity with Django views, Django URL patterns, and the command line
curl tool. In case you need a refresher, this prologue covers these topics. If you’re comfortable enough with the pre-requisites, skip ahead to the next lesson.
Here are resources for more information:
Welcome to the prologue for the Django Redirects course. The Django Redirects course expects you to be familiar with Django URLs, Django views, and at least have a high-level understanding of
00:12 If you’re not familiar with this, then follow along with the prologue and I’ll introduce some of these concepts to you. If you’re already comfortable with these topics, feel free to skip ahead and dive straight into Lesson 1.
00:23 The code samples throughout this course have been built and tested using Django 3.0 and Python 3.8. For the most part, you can get away with earlier versions—Django 2 and Django 3 are fairly similar to each other—but anything before Django 2, you might run into some problems.
simple shows the
product demonstrates an object model and how to manipulate that. The
search app is all about the
RedirectView class. The
parms app talks about parameters inside of your URLs.
This creates and runs the database migrations for the
Product object that’s used in the demonstration. As a refresher on how Django
urls work, I’m going to walk you through the
destination app inside of the
Redirector project. The
destination app contains three files:
urls file defines patterns of URLs that are associated with this app inside of the
Redirector project. The
views file contains the views that are called by those
urls in order to actually return results.
All Django views have to return one of these objects. In this case, I’m returning one with
content_type='text/plain'. This means what’s sent back to the browser isn’t HTML, which would be normally what you would do, but just some text—in this case, saying
As this course is all about URLs and how they’re redirected, I’m going to need a browser. I’ll be using the command-line tool
curl is available out of the box in most Linux and Mac installations and the more recent builds of Windows 10. If you’re not using one of those, you can find the program at curl.haxx.se.
Here’s a quick example of how to use
curl. The first one hits the Real Python website and prints out the contents of the returning HTML. The second one uses the
--include parameter to do the same thing.
Seeing as this entire course is about
302 redirects, that might be a bit of a problem. So I’ll also be using the
curl to actually follow the redirects. Now to the terminal, to see these in practice, starting with the simple example
curl plus a URL.
And that’s why I’m going to be using
content_type='text/plain' throughout most things so that you don’t have to see all of that HTML every time I call a view. So, rather than hitting Real Python, let’s go to the
Redirector Django project. I’m going to need to start the server.
This time, in addition to seeing the body
Hello World, you get to see the headers.
"HTTP/1.1" 200 tells you that the call was successful, the following six lines are additional headers associated with the content, and then finally, the body
06:43 In case you want to read any more about these topics before starting the course, here are some useful URLs. First off, if you’re new to virtual environments, the course What Virtual Environments Are Good For could help you. Secondly, there’s plenty of documentation on Django about views and URLs and how to use their patterns. Additionally, you can find plenty of tutorials at Real Python covering similar topics.
Finally, if you need
man page for
curl is there to help you. As
man pages can be a little dry, here’s an article on
curl that’s a little easier to read. Well, that’s it for the prologue! You’re ready to start the Django Redirects course.
Become a Member to join the conversation.