Django Web Application
00:00 In the previous lessons, I’ve covered command-line applications of increasing complexity. In this lesson, I’ll start web applications and talk about how Django expects you to organize your file layouts.
Django is more opinionated about project structure than generic Python. It has strong expectations about how things are organized. Fortunately, it comes with a
django-admin command that helps you create this structure fairly easily. Once you’ve done your
pip install on Django,
django-admin startproject with the name of your project will create a folder with the appropriate information in it.
That second folder contains configuration information like settings and how your URLs are going to map to the outside world. Django also gives you a copy of the
manage.py command, which helps you control your application and includes the development server that you typically use when you’re testing out your web app.
What I’ve shown you so far doesn’t include your application logic. Your application logic goes inside of modules, which Django calls
django-admin command has a
startapp to go along with the
I’ve still got the
django_world/ project route. Then, I’ve got the name of the app that I’ve just created. And inside of that, it creates stub files for all of the things that it expects inside of a Django app.
models file is for the ORM, if you have any for the app. It comes with a default
tests file for testing the app. And then finally,
views is for your model-view-controller code that outputs the actual content to the web framework. Additionally to what I’ve shown you so far, there’s other structure you can put inside as well. For bigger applications, it’s probably a good idea to have docs in there—you probably heard me talk about that enough in the previous lesson.
static/ folder is where you put your static content, like your Cascading Style Sheets.
templates/ is where you put your HTML templates, which are rendered by your views inside of your applications.
And then these last two that I’ll show you are files that don’t come with Django, but I tend to put inside most of my Django applications—
runserver are Bash scripts that are common tasks that I find I do frequently when I’m developing inside of Django.
And this is
resetdb. Oftentimes when I’m building a new Django application, I may not have decided on exactly what the database would look like, so instead of migrating and experimenting back and forth between different ORM layouts, I might want to wipe everything and start from scratch.
This script does that. The first line here removes all the
.pyc files. The second line removes the existing database. The third line runs a migration, which installs the ORM into the database, creating a fresh database.
03:40 One last thing to note is the structure of a Django project is different than a packageable Django app. If you’re doing Django apps, you have to do something a little differently, and that’s beyond the scope of this particular lesson.
03:52 Django itself is a big application framework. There’s lots of content out there and Real Python has got some good tutorials if you want to dig into Django. Of course, you can also go off to the Django project docs themselves.
Become a Member to join the conversation.