Getting the Project Started
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.
Next, install Django. Now, create a new project. Next, create an app called
core inside the project.
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.
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.
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.
Once you’ve added the app, you also need to modify the
TEMPLATES declaration to add the templates folder, which was created earlier on.
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
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.
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.
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
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
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.
Become a Member to join the conversation.