Getting Started with Django Web Development: Django Tutorial #1
This is exciting stuff. Whether you’re a seasoned coder or just beginning with creating your very first web application, I want to welcome you to the beautiful world of django web development. Django was developed as a web development framework that enables developers to very quickly make their ideas a reality – something that if you’re reading this, I bet you’re ready to do. So let’s get started with this Django Tutorial.
If you’re experienced with coding, you can definitely skip the introduction & Terminology section. If not, and Django is your first real time using Python and a Web Development Framework (It was for me) these sections will be helpful of the Django Tutorial.
Let’s go over the terminology of quite a few things that we’ll be using in these tutorials to get you up to speed.
Django uses the coding language Python. If you aren’t familar with it, I encourage you to click the link and explore a bit about it. It’s not dire that you understand everything when it comes to python itself. It’s just the language and you’ll pick it up as you go through the tutorial.
“Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.” – Django Project
Okay, that’s great and all, but what does that mean for us? Django being open source enables users to edit the framework themselves. It builds a community of developers that are continually improving Django and making it even better to use. This is one of the reasons I chose Django to begin building my web applications. It will continually get better and there’s a great community ensuring that.
So let’s just clear up some things we’ll be using before we run into them:
Python – Coding Language
Django – Web Framework running off of the Python Language
HTML – ‘HyperText Markup Language’ This will be what we develop our pages in.
CSS – ‘Cascading Style Sheet.’ CSS will enable us to have better looking web pages. It adds unique elements to your HTML files.
Database – A database is what will store information that you wish to store. Django utilizes a relational database, we’ll dive into this a bit later. But just know that databases store information right now.
SQL – ‘Structured Query Language.’ This is the language we will have to use to be able to reference, or query, the databases we create. So if we want to pull information out, add it in, or manipulate it, we will be using SQL.
Django Project – This will be the entirety of your website. The Django project will usually include many “apps.”
Django App – This is a component of your django project which serves one specific functionality. For example, you could have one app for allowing users to signup for your website, and another for a blog functionality.
- It is important to note here that an app is not the entire website. This is something that is different in a lot of other web development contexts. An ‘app’ to Django is just one piece of the whole website.
So let's get started.
First we’ll need to get the software that we’ll be using. If you’re just getting started, you absolutely need to use Python Version 3, at least. We’ll be using version 3.6 as that is the most up to date version at the time of writing this article. I suggest you use the most up to date one yourself as updates only make python better and safer for users.
Do not use version 2 as support for this will be ending in the coming years. You can download python at the link found here.
Next, we’ll need to get Django. The current version at the time of this article is version 2.0.3. To get Django, you’ll need to open your command prompt or your terminal (windows will be command prompt and mac users will be using their terminal.) I’m using a mac, so all my screenshots will be from the Terminal.
Open your Terminal / Command Prompt and type the following without the ‘$’ character:
$ pip install Django==2.0.3
If you’re new to using the command prompt or terminal, the ‘$’ character just denotes that this code is being placed into a command prompt or terminal.
If you’re having trouble with this step, you can check the Django Tutorial install guide four here.
After the install, you can check to make sure a successful install by opening a python shell, importing Django, and using it’s version function.
Notice I made an error trying to import Django as ‘Django’ and not ‘django’. Rookie mistake – I know. But none the less, we first called python by typing ‘python’. This opened a python shell, then we import django and called its function ‘VERSION’ to check it’s version. At the time of this article 2.0.2 is the most up to date, so that’s what we’re using.
Picking your code editor.
This is one of the biggest decisions of your coding career. There’s so many good options and they all do pretty much the same thing. However, some are better prepared at saving you time and heartache than others. I began coding a few years ago and really enjoyed the Sublime text editor.
However, like most first loves, we just weren’t compatible as we grew older. While I’m sure most of my struggles are resolved now in the newer versions, I found my new code editor soul mate in Atom Text Editor.
There’s plenty of good text editors out there, so feel free to roam and find your own text editor soulmate. All my screenshots will be through the atom text editor.
Starting your first Django Project
Open up your terminal or command prompt and get excited. We’re about to begin our Django Journey.
First, on your desktop, go ahead and make new folder, or directory, to store our django project.
Then we will cd our terminal into that folder and type the following command:
$ django-admin startproject ProjectName
Where ‘ProjectName’ is just the name that you want your project to have.
django-admin is just specifing that you want to use django admin control, and ‘startproject’ is the command django uses to start a new project. Remember, a project will be our entire website. An App will be one component of the project. Below is what it look slike in the command prompt.
I named my project ‘Example.’
If you’re completely new to the terminal, look at how I changed my directory in the above example as well. I was working from my ‘derrickmac’ directory in the first example, so I wrote cd (change directory) to desktop, and then into my folder ‘Derrick’ that I created for this tutorial. Let’s check inside the folder and make sure it worked.
We see that startproject created a few things inside our folder. My Project I called ‘Example’ so django created an ‘Example’ folder inside my folder ‘Derrick’ and then inside ‘Example’ it created another folder by the same name and a manage.py file.
Then inside Example/Example it created a few more python files. Let’s briefly go over what each of these files do.
- manage.py is a file which contains over project commands. You’ll use this file quite a bit while testing your server locally and while using databases.
- __init__.py is a file that you will likely never touch in the beginning stages. All it does is specify that this folder should be recognized as a python module. Imagine it as a big red flag that says ‘I’m here, use me.’
- Settings.py is a file which includes a lot of information about our project settings. Such as Directories, the database we’re using, etc.
- urls.py will allow us to specify urls for our project and what we should do whenever a user lands on a specific url.
Creating our first app within our project
Now, we need to go ahead and make our first app within our project. The next tutorial will then ive into actually making something functional on our project. You’ll need to CD into the first layer of your project. So for my example, we’ll change the diretory to the first ‘example’.
To create an app, we’ll use the same django-admin and use the function startapp.
it’ll look like this:
$ django-admin startapp AppName
This will make the app ‘AppName’ in our project. I’ll call mine ‘Appone’ Here’s what it’ll look like in the terminal:
Now we have something that looks like this in the folder:
Brief overview before the next page:
So what have we accomplished so far?
We’ve got Python and Django.
We have created our Django Project
We’ve created our First Django App
I’d say we have a pretty good start to making our first website. In the next session we’ll actually get a website page and be able to view it. We’ve got the tools, now we just have to learn how to use them.