Let’s learn by example.
Throughout this tutorial, we’ll walk you through the creation of an initial Django application.
We’ll assume you have Python & Django installed already. If it isn’t, you should see previous our section Django Introduction & System Setup
This tutorial is written for Django 3.2, which supports Python 3.6 and later. If the Django version doesn’t match, update Django to the newest version. If you’re using an older version of Python, check What Python version can I use with Django? to find a compatible version of Django.
If this is your first time using Django, you’ll have to take care of some initial setup. Namely, you’ll need to auto-generate some code that establishes a Django project – a collection of settings for an instance of Django, including database configuration, Django-specific options and application-specific settings.
From the command line, cd into a directory where you’d like to store your code, then run the following command:
django-admin startproject myfirstsite
This will create a myfirstsite directory in your current directory.
Let’s look at what startproject created:
myfirstsite / manage.py myfirstsite / __init__.py settings.py urls.py asgi.py wsgi.py
These files are:
• The outer myfirstsite/ root directory is a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
• manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
• The inner myfirstsite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. myfirstsite.urls).
• myfirstsite/__init__.py: An empty file that tells Python that this directory should be considered a Python package.
• myfirstsite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
• myfirstsite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. (list of entry point to your site)
• myfirstsite/asgi.py: An entry-point for ASGI-compatible web servers to serve your project. (This file will be used for deploying your site to live)
• myfirstsite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project (This file will be used for deploying your site to live)
Now, it’s a time to re-check this below diagram MVT request and response calls,
You’re going to see what happens when you type an address into your web browser and press Enter.
The first address point inside the Django app is the urls.py in the myfirstsite app. That file will direct you towards the urls.py in the app you want to go to. That will forward you to views.py, where the code logic happens. The functions in there will direct you towards a specific template to render and return to the user to see in the browser.
If these lines are not clear. don't worry, you will see an example in the next session.
Let’s verify our Django project works.
The outer myfirstsite / root directory is a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like. So, I am changing it to tutorial1.
And move into the outer tutorial1 directory
And run the following commands: (This command will start the development server)
python manage.py runserver
You’ll see the following output on the command line:
Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). You have 18 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. August 29, 2021 - 19:28:27 Django version 3.2.6, using settings 'myfirstsite.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.
Note: Ignore the warning about unapplied database migrations for now; we’ll deal with the database shortly.
You’ve started the Django development server, a lightweight Web server written purely in Python. We’ve included this with Django so we can develop things rapidly, without having to deal with configuring a production server – such as Apache Tomcat – until you’re ready for production.
Now that the server’s running, visit http://127.0.0.1:8000/ with your Web browser. You’ll see a “Congratulations!” page, with a rocket taking off. It worked!
You will see an additional db.sqlite3 file created when we run command python manage.py runserver. We will see this file in the database session.
By default, the runserver command starts the development server on the internal IP at port 8000.
If you want to change the server’s port, pass it as a command-line argument. For instance, this command starts the server on port 8080:
python manage.py runserver 8080
If you want to change the server’s IP, pass it along with the port. For example, to listen on all available public IPs (which is useful if you want to show off your work on other computers on the network), use:
python manage.py runserver 127.0.0.0:8000
The development server automatically reloads Python code for each request as needed. You don’t need to restart the server for code changes to take effect. However, some actions like adding new files don’t trigger a restart, so you’ll have to restart the server in these cases.
Congratulations! You have successfully completed initial Django application setup tutorial session.
If you have any doubts or queries related to this chapter, get them clarified from our Python Team experts on ibmmainframer Community!