Press Ctrl+C to stop any running webserver.
In the terminal enter the command below to create a django app named game_ranks.
python manage.py startapp game_ranks
This will create a folder _game_ranks with some files for the app.
We will use this in this example to make a django based website about game rankings.
Below you can see the files that have been created.
The five main areas are listed below
Next we will outline the purpose of each of these sections
This contains the functions that are used to define the pages in your web app
This contains the classes that define your data for the web app.
This is for the configuration of the application
This is used for creating an administration interface.
This is used to manage different database versions.
Creating your first page
Modify views.py in the project folder to display some html.
from django.http import HttpResponse from django.shortcuts import render # Create your views here. def home(request): return HttpResponse("<h1>Game Ranks</h1>")
Create a new file called urls.py in the project folder game_ranks.
This file is used to map out the patterns between the view functions and the urls for the site.
Note on the second line game_ranks is the name of the project folder.
We have created a path to the root of the website. This is done on line 6. This will run the views.home function.
from django.urls import path from game_ranks import views urlpatterns = [ path("", views.home, name="home"), ]
Now we need to modify the django server configuration. This is in the example_web_project folder. It also has a urls.py file. Open this.
Modify the code so that it includes the lines:
- from django.urls import include, path
- part(“”, include(“game_ranks.urls”)),
as shown below:
from django.contrib import admin from django.urls import include, path urlpatterns = [ path("", include("game_ranks.urls")), path('admin/', admin.site.urls), ]
This will include the include and path functions and allow us to link to the game_ranks urls.py file.
Run the server with the following command and then view it in the browser.
python manage.py runserver
You have now created your first webpage in django.
We will now add a configuration so we don’t have to type the runserver command everytime we want to run the web app.
Go to Run > Add Configuration
Save the file.
Now when you Press F5 or Run > Start Debugging the webserver will run.