How to Create a Dynamic Web App with Django

How to Create a Dynamic Web App with Django

Overview

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

Install Django

First, use the pip package manager to install Django.

$ pip3 install django

You may want to upgrade pip at this point.

$ python3
>>> 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:

Kamatera
$4.00 /mo
Starting price
Visit Kamatera
Rating based on expert review
  • User Friendly
    3.5
  • Support
    3.0
  • Features
    3.9
  • Reliability
    4.0
  • Pricing
    4.3
Ultahost
$2.50 /mo
Starting price
Visit Ultahost
Rating based on expert review
  • User Friendly
    4.3
  • Support
    4.8
  • Features
    4.5
  • Reliability
    4.0
  • Pricing
    4.8
Hostinger
$2.99 /mo
Starting price
Visit Hostinger
Rating based on expert review
  • User Friendly
    4.7
  • Support
    4.7
  • Features
    4.8
  • Reliability
    4.8
  • Pricing
    4.7
  • Want to get top recommendations about best hosting? Just click this link!

How to Create a Simple Web Application with the Django Web Framework

Django makes creating a web application relatively easy. This article shows you
less than a minute
Lindsey Conger
Lindsey Conger
Author

How to Install the Django Web Framework on a Ubuntu 22.04 VPS

Brief description When building a website, similar components are required, and
less than a minute
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to install the Django Web Framework on Windows

For the windows enthusiast, you can install Django on windows.
less than a minute
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to install Django on a CentOS 7 VPS or Dedicated Server

When building a website, similar components are required, and you do not have to
less than a minute
Mark Armistead
Mark Armistead
Author
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top