How to get a Django web app up and running.
Confirm you have python installed:
If you have python installed, the above command will return the version you are using, for example:
If you do not have python installed, visit python.org and install for your platform.
From the command line, create and navigate into the directory where you want to create the new web app:
cd c: mkdir django-example cd django-example
Create a python virtual environment inside the new directory:
python -m venv .venv
Once the above command has finished (which may take a minute or so), run the following command to activate the virtual environment:
Install the latest version of Django from the command line:
pip install django
Now we have Django installed, we need to create a new app within our project (projects can contain a number of apps).
From the command line, create a new app within our project called ‘config’ as this intial app will mostly contain files relating to the settings of our project:
django-admin startproject config .
We include the ‘.’ at the end of the command as it prevents creating an external folder like this: /django-example/config/config which you might need to restructure and remove when it comes to deployment.
Once the config app is created, our project structure will look like this:
Lets take a look at what of each of these new files are:
config/is the external folder of your project. It doesn’t matter to Django, so you can actually rename it to whatever you want.
config/config/is the actual Django project folder. It contains the setting files of your project.
manage.pyis a Python script with the same functionality of
django_adminbut uses your project’s settings.
config/configa Python package.
settings.pyis the core file of your project. You can add, modify, or delete variables to change the behaviour of your project.
urls.pyis the file that defines the URLs of your project.
wsgi.pylet you deploy your project to a server.
Django comes packaged with basic database setup, IF you’ll be using the basic setup its a good idea to run these migrations now:
python manage.py migrate
Once the migration has finished fire up our Django web app:
python manage.py runserver
Once the app is up and running you’ll see the following command line info:
Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). February 17, 2023 - 08:45:09 Django version 4.1.7, using settings 'config.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.
You can now visit http://127.0.0.1:8000/ from your browser: