Practical Guide to Python

Setup Django Blog Project

Practical Guide to Python

Check out a free preview of the full Practical Guide to Python course

The "Setup Django Blog Project" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina installs the Django framework and uses it to create a basic blog application. The migrate command uses the manage.py file to initialize the database. The runserver command starts the local development server.

Preview
Close

Transcript from the "Setup Django Blog Project" Lesson

[00:00:00]
>> So, Django, since this course is targeted towards more of an advanced beginner, we're going to jump right into Django. If you'd rather learn about Flask, you can watch the last chapter of the intermediate Python course on front end masters, or you can review the final exercise on the course website.

[00:00:19]
So we're going to go ahead and grab the project code instead of starting a Django project from scratch. If you do want to start a new Django project from scratch, I recommend that you follow the steps on the official Django tutorial for building your first app. That has everything that you need to start up a Django project, but for the time being, we're going to work from a project that I have already created.

[00:00:48]
So go ahead and CD into your home directory and get clone this project right here. And then let's CD into the practical blog, Directory. One more thing, if you're not doing this in a new tab, make sure that you type deactivate and clear out the current active virtual environment.

[00:01:14]
We're going to create a new virtual environment for this project because it doesn't share any of the libraries that we have been using in class before, and we wanna keep everything separate. Oops, that was already in the right place. So now you should have a directory that looks something like this.

[00:01:41]
And we're gonna go ahead and create a new virtual environment. And then we're going to use that pip install -r command to install what is in requirements.txt in it. So to make a new virtual environment, remember, you do python3 -m venv. And then we're going to source it, so source env/bin/activate.

[00:02:08]
And we'll see that now I have my new virtual environment activated. If I take a look at what is in requirements.txt, see that it is a file with just one line and it said the requirement is Django 3.1.2. So I can type python-m pip install-r, which means install the requirements from this file and pass in requirements-txt.

[00:02:34]
And that's gonna go ahead and install Django. While we're at it, we can also run python-m pip install--upgrade pip to be on the latest version of pip so that we don't see this annoying warning anymore. There are two things that we're going to need to do to get set up to run this web application.

[00:03:01]
We're going to need to prime our database by running a migrate command, and we're going to need to run the server to make sure that our server has come up successfully. So to prime the database, you're gonna do python manage.py, along with the migrate command. manage.py is the file that's automatically generated by Django and has shortcuts to a lot of the commands that you might want to run when working with a Django application.

[00:03:33]
You should see something like this, operations to perform, apply migrations. Most of these, four of these, admin, off, content type, and sessions come baked into Django. And then blog is the app that I've created. So this goes ahead and creates all of the database tables that Django needs to operate in a file called db.sqlite3.

[00:03:59]
That's because we're currently working with a local SQLite database. When you're working in production, you can move on to MySQL, or Postgres, or whatever database you want to use. Finally, we can run python manage.py run server and see if our server comes up successfully. If it does, you should see it running at localhost8000.

[00:04:24]
And when I visit that URL, I'll see My Python blog, no posts yet, come back later. So I'm just gonna give three or four minutes for folks to catch up with me, and make sure that they're able to start their Python server and see the same thing that I'm seeing.

[00:04:42]
Instructions for that should be here. I did notice one typo here. Unfortunately, to make a new virtual environment, make sure that you use Python3 and not Python. And I will go ahead and fix that when I have a moment.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now