Hello, world!
Building web applications with Python & Django
🕑 This lesson will take about 20 minutes
Running your server
Now that you’ve set up your first project and app, you test that it is working by starting the web server.
Run the following command in VS Code’s Terminal (if you have any issues, make sure DEBUG is set to True in your settings.py file):
python3 manage.py runserver
Note, if using Windows you might just need python instead of python3 at the start of the command.
This command will set up a development server (dev server) to use. After running the command, you’ll get a warning message saying “You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions…”. Don’t worry about this for now. This isn’t relevant to what we’re doing right now and isn’t important until we start setting up our database. You can ignore it for now!
After running the command, you should see a message in the Terminal that says something like “Starting development server at http://127.0.0.1:8000/ ”. Open the URL that your server is running on in a new browser window. You’ll see a default landing page that says something like “The install worked successfully! Congratulations!”
Configuring URLs and views
Now that the server is running, the next step is to configure URLs for the different “views” in your app (views are pages/screens that will be displayed to the user based on what they have requested). We don’t have any configured URLs yet (or URL confs).
URL confs, also commonly known as routing means the following: When the user visits a URL (eg. website.com/login) either by clicking a link, submitting a form, etc., the server will run code in response to that and “route” the user’s request to a function or method that will handle the request and perform a desired action (eg. fetching data from the database and displaying results, or displaying a particular page to the user).
In VS code, go into the myapp folder and then open the file views.py - this file contains the code that will be used to display information to the user. At the top of the code, modify the import statement to add HttpResponse to the import statement. It should now look like:
from django.shortcuts import render, HttpResponse
In the same views.py file, create a new function called index() and add the following code to it:
def index(request):
return HttpResponse('Hello, world!')
This will be your first view (displaying a “Hello, world!” message to the user). The request parameter in the index() function will contain all the information from the user’s request (eg. submitted form data, queries, etc) that we can use. We won’t be using this information just yet but it will be useful later. For now, we’ll just send a basic text-based response to the user.
Note: index represents the homepage of the website/web application (the first page people will see when they visit the root directory eg. website.com/).
So now we have a view set up, but we also need to setup our URL configuration so that the website knows to use this view when a certain URL is requested.
Create a new file called urls.py within the myapp directory (you will notice there is already a urls.py file in the project directory. However, to keep things organised for different sections of our website/application, we will use a separate urls.py file within each app directory (eg. myapp).
In the new urls.py file in the myapp directory, create a list called urlpatterns - this is where all the different paths/routes for the app will be set up. The first one is for the home page (an empty string). Use the following code:
The file should now look like this:
Now go to the global urls.py file in the project folder/directory to register this url configuration in the global project configuration. Add include to the from django.urls… import statement and then add path('', include('myapp.urls')) to the urlpatterns list.
What will happen is if the user requests the admin page, that will be displayed. But if that is not requested, it will point to the URL configuration in the myapp directory, which will then look for the requested route (eg. the index/home page) and then display the appropriate view to the user.
Here is the complete code so far for the global urls.py file in the project directory:
Save all your code and launch the web server’s home page (eg. http://127.0.0.1:8000 ) in your browser again (or refresh the page if it is already open). The page should now display a “Hello, world!” message to the user.
Next lesson: Declaring your first models