As a “batteries included” framework, Django provides everything you need to create a secure and easy-to-maintain web application.
Because Django abstracts away a lot of the low-level details, is well suited for beginners, though it’s highly regarded by experienced developers who can quickly develop working web applications with Django.
Django’s flexible enough that you can use it for everything from a simple website to a massive, complex project.
It’s best to know at least some basics of Python before learning Django. If you’ve not coded with Python before it’s an excellent language for beginners to learn, and there are a lot of excellent tutorials to help you get started.
Django’s a very popular web framework backed by a vibrant community, improving the software, writing documentation, and helping people solve problems.
Special note: consult HostAdvice’s Best Python hosting and Best Django Hosting pages to find the leading web hosts in these categories, including expert and user reviews.
Before you start
This article assumes you have some knowledge of Python, have installed Python, and know how to create and use a virtual environment. These two articles can get you started setting up Python and learning Python: <how to install python and create a virtual environment> and <How to write your first Python scripts.>
Set your virtual environment
$ python3 -m venv virtenv
$ source virtenv/bin/activate
First, use the pip package manager to install Django.
$ pip3 install django
You may want to upgrade pip at this point.
>>> import django >>> print(django.get_version()) # This should return the Django version number. 2.0.4
You’re now ready to build something using Django!
Create your Django project
You can call your project whatever you want, but it’s best to keep it simple. Change to the directory in which you’d like to create your project. For the purposes of this project we’ll created a directory for the project called web.
Start your Django project!
$ django-admin startproject website
This command will create a directory called hello and the initial project files. Do a directory listing to see what’s there.
You’ll find that Django created files in the current directory website and created another directory website with additional files. Here’s the structure.
website/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py
The first website directory is the directory where you’re keeping your project. You can change the name to something else if you’d like.
You’ll use the manage.py utility to manage your application. Within the second website directory, _init__.py tells Python what packages to use for this project. It starts out empty.
The settings.py script enables you to changed and add configuration settings to your project, urls.py lets you set the routes for URLs on your website, and wsgi.py (Web Server Gateway Interface) serves as the entry point for a web server so your site will serve content.
You’ll learn more details about all of these files and more as you work with Django, but now let’s try this thing out.
Test your Django server
$ python manage.py runserver
It should return something like the following.
Performing system checks... System check identified no issues (0 silenced). You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them.
April 26, 2018 - 18:51:18 Django version 2.0.4, using settings 'website.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
Ignore the errors as you’re just testing your server. Enter http://127.0.0.1:8000/ into a browser address bar.
You’ve now successfully created a project and tested the server. The website project will now serve as the container in which you’ll create your apps.
Create your Django “Hello World” view
Making sure you’re in the directory containing manage.py, run the following command.
$ python manage.py startapp hello
This command will create a directory called hello for the hello app with this structure:
hello/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py urls.py views.py
Open hello/views.py with your favorite editor. Add the following to view.py then save the file.
from django.shortcuts import render # Create your views here. # Note: The stuff above this will already be in this file. from django.http import HttpResponse def index(request): return HttpResponse("Hello, world!")
Next, create a URL configuration (called a url.conf) by adding a file named urls.py in the hello directory. Add the following code to hello/urls.py and save the file.
from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
Next, you’ll need modify the website url configuration by editing website/urls.py. Add the following code to the bottom.
from django.contrib import admin from django.urls import include, path urlpatterns = [ path('hello/', include('hello.urls')), path('admin/', admin.site.urls), ]
Start your server.
$ python manage.py runserver
Then enter this URL into a browser address bar: http://127.0.0.1:8000/hello/
If your web browser returns a simple “Hello World” you’ve successfully created your Django Hello world app! You’ve learned how to start a project, test your server, and create a simple working application.
The next article will help you build on this by creating an interactive app for this project.
Check out the top 3 Django hosting services:
- Want to get top recommendations about best hosting? Just click this link!