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.

Getting the Project Started

Download

Sample Code (.zip)

17.9 KB
Download

Course Slides (.pdf)

1.4 MB

00:00 Getting Started. To better understand authorization, you’ll need a project to experiment with. You’ll run through the creation of a simple blog app to demonstrate the techniques needed. Alternatively, you can follow along by downloading the sample code from this link.

00:19 All the demonstration code was tested with Python 3.8 and Django 3.1. It should work with other versions, but there may be subtle differences. Creating a Project.

00:32 First, you’ll need to create a new Django project. Since Django isn’t part of the standard library, it’s considered best practice to use a virtual environment.

00:41 You can see one being created here and activated using the commands onscreen.

01:05 Next, install Django. Now, create a new project. Next, create an app called core inside the project.

01:32 Now, add a templates/ directory to the project. Next, perform the migrations that are needed. And finally, for the moment, create a site superuser, giving you access to the admin area.

02:05 You now have a Blog project, but you still need to tell Django about the app you created and the new directory you added for templates. You can do this by modifying the Blog/settings.py file, first by changing the INSTALLED_APPS list to include the core app you created earlier on.

02:26 Don’t forget that it’s good practice to put a comma (,) at the end of each entry, as this minimizes the amount of changes in any source control system you’re using.

02:34 Once you’ve added the app, you also need to modify the TEMPLATES declaration to add the templates folder, which was created earlier on.

02:49 This tells Django where to look for your templates. Note that this code is an area which is specific to Django 3.1 and later, as it moved to using pathlib rather than the os library.

03:02 If you’re following along using an earlier version of Django, then you’ll need to modify your code to suit the syntax that the os library uses.

03:12 The sample site you’ll be working with is a basic blogging application. The core app needs a models.py file to contain the models that store the blog content in the database. Edit core/models.py and add the following.

03:38 Now for some web pages. Create two views, one for listing all the blogs and one for viewing the blog. The code for your views goes in core/views.py.

04:35 The listing() view does a query looking for all the Blog objects and passes that to the render() shortcut function. render() takes the request object that provides context to the view, the name of a template to render, and the data object containing the query set of Blog objects.

04:54 The view_blog() view takes a blog_id as a parameter and uses it to look up the corresponding Blog object. This object is passed as context to the view_blog.html template through the render() shortcut. In the next section, you’ll see how templates are constructed in Django and complete the setup of the blog site.

Gandalf on Jan. 10, 2024

Why adding a comma (,) at the end of each entry, minimizes the amount of changes the source control system?

Geir Arne Hjelle RP Team on Jan. 10, 2024

Hi Gandalf,

consider a case where you have several items, each on its own line, but without the comma after the final item:

items = [
    "item 1",
    "item 2",
    "item 3"
]

If you later need to add a fourth item to your list, you need to add a comma after "item 3" which means that the version control system will consider that line changed, even though you’re not really doing anything to "item 3".

If you already had a comma after your final item, you won’t need to change that line to add a new item.

Become a Member to join the conversation.