Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Course Prologue

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:

00:00 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 curl.

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.

00:37 I’ve built a Django project called Redirector which is going to be used as the sample code throughout this course. It’s available in the source materials dropdown in the description.

00:48 There are five apps inside of the Redirector. The first one is destination. This is the app that I’ll be redirecting to. Then there’s simple, product, search, and parms.

01:01 simple shows the redirect() shortcut. 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.

01:17 Once you’ve downloaded the sample source code, if you wish to run it, you’re also going to need to install Django. Preferably you should do this inside of a virtual environment.

01:26 I’m using the virtualenv tool to create a clean virtual environment. You might use pyenv or others, but the concepts are similar. Inside of this clean environment, I install Django.

01:37 Once I’ve got Django installed, there’s some database work that needs to be done. I’ve combined those commands inside of a shell script called resetdb, inside of Redirector.

01:48 This creates and runs the database migrations for the Product object that’s used in the demonstration. As a refresher on how Django views and urls work, I’m going to walk you through the destination app inside of the Redirector project. The destination app contains three files: urls, views, and __init__.

02:09 The 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.

02:27 Here are those files together. In the top half, you see the views file from the destination app. In the bottom half, the urls file of the destination app. These two files work closely together.

02:39 Line 7 of the urls file defines a URL pattern called 'hello/'. Inside of a browser, when you type in the address ending with hello/, this pattern is matched inside of destination.urls.

02:50 The match tells Django to look up a view. The view, in this case, is the views file defined in the destination app and the function hello_world(), inside of that.

03:01 You can see this function’s definition in the top window. Line 4 and 5 shows the hello_world() view. It returns an HttpResponse object.

03:13 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 "Hello World".

03:30 Throughout the course, I use content_type='text/plain' in order to keep the responses small and short, and not have to have the extra HTML around it.

03:42 The destination.urls file gets included in the master urls file in the Redirector’s definition I’m including all of the urls defined in destination.urls at the root of the path.

03:54 That means both 'hello/' and 'destination/' are available at '/' inside of the pattern. If you hit the development server, http://localhost/hello will map to the hello_world() views.

04:12 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. 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.

04:33 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.

04:46 --include says, “Return the body as well as returning the header content.” By default, curl doesn’t follow 302 redirects.

04:57 Seeing as this entire course is about 302 redirects, that might be a bit of a problem. So I’ll also be using the -L parameter. -L tells curl to actually follow the redirects. Now to the terminal, to see these in practice, starting with the simple example curl plus a URL.

05:20 Did you catch that? Well, it was almost 700 lines of content. curl, when hitting a URL, returns whatever your browser would get back. In this case, the entire HTML page of realpython.com.

05:34 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.

05:49 There’s a script called runserver inside of it. It just calls manage.py runserver.

05:55 using curl to hit 127.0.0.1localhost/hello/ hits the destination app I was showing you earlier.

06:04 The server shows /hello/ being called. It says that it returned a 200which is success—and curl shows the result: the Hello World string.

06:17 Doing that again with --include.

06:23 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 Hello World.

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.

07:09 Finally, if you need curl, the 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.