Starting with Django 4.0
Django can be intimidating for first timers. Trust me, I know this because I've spent a lot of time trying to learn it as best as I can by reading books. Thankfully, I know enough to help you get started.
This first tutorial will deal with getting Django set up, along with a virtual environment. Part two this tutorial series will configure Django to create a "Hello, world!" message.
Django is available for most systems, but this tutorial was written on a Mac. Regardless of this, the initial steps to get started with Django are the same:
1) Set up a virtual environment (this tutorial uses pipenv)
2) Activate the virtual environment
3) Install Django
4) Run Django for the first time
This tutorial is made for beginners. Anybody with intermediate experience with Django will find this boring.
Virtual Environment
Always use a virtual environment like pipenv whenever you're beginning a Django project. This helps isolating an environment in case you have a specific version of Django (or other Python module) you want to use.
Establish a folder for this tutorial. I've called mine hello-world-project. Navigate to that folder and run the following in your command line:
pip install --user pipenv
This will install pipenv if you don't have it.
After your terminal shows you the installation is done run the following command in order to activate the shell.
pipenv shell
Your terminal should display the name of the directory you're in within parenthesis. This means that you've successfully activated the virtual environment.
To exit at any moment, type the word exit.
Next, install Django using pipenv.
pipenv install django
Once the terminal's indicated that you've successfully installed Django!
Django Runserver
The next step in this process is to create a Django project called hello-world and then run it on the local server.
Back in the terminal, type the following:
django-admin startproject hello_world .
Notice the period after "hello_world". It's important to include this.
The terminal should quickly go back to your control. Type in the shell command ls
and there should be a hello_world directory containing your recently created Django project, a file called manage.py, and then the Pipfiles that contain the information on your virtual environment.
This tutorial won't go in depth about what this just yet. If you're curious about what all this is the Django Foundation goes into this in depth. In later tutorials I will provide summaries of how Django project is structured but for now just follow along.
Alright, I'm a big believer in small victories to reinforce learning, and luckily, Django provides just that. Run the following command in your terminal:
python3 manage.py runserver
Ignore the 18 migrations message for the moment. Those will be dealt with in the second tutorial.
You should see a prompt that says that there's a development server at: http://127.0.0.1:8000
Navigate to that and Django's greeting page should appear for you.
Success! If you've made it this far then you can rest assured that Django works properly.
Part two of this tutorial will expand on this foundation and create a basic "Hello, world!" html page to appear.
Top comments (0)