In parts 1 and 2, I laid out the basic structure of Django as well as a simple explanation of its workflow. Part 3 is the first article, of many, in which I will try and walk you through the necessary steps in building a simple web application.
To keep things simple, the application we will build is a website with a home page as well as a user registration form. Also, we will wrote the necessary code for an automatic email send to the user confirming his/her registration. I believe this application is a great starting point. I only wish I will be able to walk you through it conveniently.
Lets get started!
Open your terminal and create a new folder for your project
mkdir djangoProject
cd djangoProject
then create a virtual environment and activate it.
virtualenv djangoEnv
source djangoEnv/bin/activate
If you don't know what a virtual environment is, please read this doc. It does a much better job explaining what it is then I ever will. The biggest advantage I find from a VE is that I don't have to worry about keeping track of all the dependencies, and I'm able to move the project easily onto another system.
Moving along, create the run the command below to create the project folder and management script.
django-admin startproject website
cd website
This will create a skeleton directory as shown below
The settings.py file is where all the website settings are stored. This is also where we register any new apps we build, as well as configure all the databases we will be using.
wsgi.py is boilerplate code to facilitate communication between your apps and the web server. You won't have to worry to much about this file.
urls.py is where all the URL mapper functions will be stored as we discussed before in the views article.
If you open the manage.py file, you'll see that the script written inside allows any command written after this file in the terminal, to act as if it is written in the django command line. So every time we write
python manage.py _______
Whatever is written in the blank spot will be ran from the django command line.
We will use this to start the webserver, open the shell to interact with the database and more importantly to create apps, like we will now. Go ahead, make sure you're in the directory 'website' and type the following
python manage.py startapp websiteApp
Your directory should now look like this
Most of the files created, we've talked about before. The migrations folder is used to store all the migrations we will make. These files automatically update the database as we modify our models in models.py
Settings.py
First thing you always want to do when creating a new app is to configure, and register it. You can think of the website folder (the second one) as the admin folder where all the applications you create are managed on a top level.
Open the settings.py file located in the website folder. Register the app by adding it to he list of INSTALLED_APPS
It should look like this
The next thing you want to do is scroll down and ensure the correct TIME_ZONE. There is no correct answer for this, it's totally up to you and the application at hand.
One last necessity is ensuring you have the correct database configured. In settings.py you will find the following
Now that we've got the basics set up, lets start building this App! Continue onto part 4 here.
To keep things simple, the application we will build is a website with a home page as well as a user registration form. Also, we will wrote the necessary code for an automatic email send to the user confirming his/her registration. I believe this application is a great starting point. I only wish I will be able to walk you through it conveniently.
Lets get started!
Open your terminal and create a new folder for your project
mkdir djangoProject
cd djangoProject
then create a virtual environment and activate it.
virtualenv djangoEnv
source djangoEnv/bin/activate
If you don't know what a virtual environment is, please read this doc. It does a much better job explaining what it is then I ever will. The biggest advantage I find from a VE is that I don't have to worry about keeping track of all the dependencies, and I'm able to move the project easily onto another system.
Moving along, create the run the command below to create the project folder and management script.
django-admin startproject website
cd website
This will create a skeleton directory as shown below
website/
manage.py
website/
settings.py
urls.py
wsgi.py
The settings.py file is where all the website settings are stored. This is also where we register any new apps we build, as well as configure all the databases we will be using.
wsgi.py is boilerplate code to facilitate communication between your apps and the web server. You won't have to worry to much about this file.
urls.py is where all the URL mapper functions will be stored as we discussed before in the views article.
If you open the manage.py file, you'll see that the script written inside allows any command written after this file in the terminal, to act as if it is written in the django command line. So every time we write
python manage.py _______
Whatever is written in the blank spot will be ran from the django command line.
We will use this to start the webserver, open the shell to interact with the database and more importantly to create apps, like we will now. Go ahead, make sure you're in the directory 'website' and type the following
python manage.py startapp websiteApp
Your directory should now look like this
website/
manage.py
website/
websiteApp/
admin.py
apps.py
models.py
tests.py
views.py
__init__.py
migrations/
Most of the files created, we've talked about before. The migrations folder is used to store all the migrations we will make. These files automatically update the database as we modify our models in models.py
Settings.py
First thing you always want to do when creating a new app is to configure, and register it. You can think of the website folder (the second one) as the admin folder where all the applications you create are managed on a top level.
Open the settings.py file located in the website folder. Register the app by adding it to he list of INSTALLED_APPS
It should look like this
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'websiteApp.apps.WebsiteappConfig',
]
The only line you will add is the one in bold at the bottom. All the other ones are already pre-installed for you. We will discuss the usage of each at the appropriate timing. The next thing you want to do is scroll down and ensure the correct TIME_ZONE. There is no correct answer for this, it's totally up to you and the application at hand.
One last necessity is ensuring you have the correct database configured. In settings.py you will find the following
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Fortunately, Django is flexible enough to work with almost all database engines. All you must do is configure it by accurately specifying the ENGINE and NAME parameters. Which are, respectively, the type of database engine and it's path on your system. If you've delved into other types of web frameworks in industry use, you'll appreciate the simplicity Django offers.Now that we've got the basics set up, lets start building this App! Continue onto part 4 here.