Don't already have an account? Create an account.
By creating an Ordinary Coders account, you agree to Ordinary Coders' Terms and Condtions.
If you already have an account, login instead.
May 20, 2020, 11:50 a.m.
· 4 min read
Last Modified: Oct. 5, 2020, 3:22 p.m.
Configuring your Django web app is a necessary part of rendering your own custom templates.
If you haven't installed Django in a virtual environment check out the Quick Start to Django Installation before continuing below.
This article will be a brief Django configurations tutorial where you will create and view your HTML and Python files.
You will need a text editor to complete this tutorial. We recommend the free version of Sublime Text.
How to open your Django project in Sublime Text
Sublime > env
Open the Sublime application then click File > Open Folder ... in the upper left corner. Select your virtual environment folder, in this case env, on your computer and open it in Sublime.
Create a Django templates folder for your HTML templates
env > mysite > main > (New Folder) templates > (New Folder) main > (New File) home.html
Click on the mysite folder then the application folder called main. Create a new folder named templates in the mysite > main folder.
Within the templates folder create a new folder called main. The templates > main folder will hold all of the HTML files created for your application.
Create a home.html file
env > mysite > main > templates > main > home.html
Finally, create an HTML document named home.html within the templates > main folder. Add the HTML <p>Hello, world!</p> to the file and save.
Add the app main to settings.py
env > mysite > mysite > settings.py
Now we need to add the app main to the list of installed applications. Under INSTALLED_APPS add 'main.apps.MainConfig'. Save the changes to the file.
Create the main URLs
env > mysite > main > (New File) urls.py
from django.urls import path
from . import views
app_name = "main"
urlpatterns = [
path("", views.homepage, name="homepage"),
We have the Django project, app, and homepage but we still need a way to render the custom HTML template in the browser via a URL.
Create a new file called urls.py in the main application folder. It will appear just below the tests.py file once created.
Within main > urls.py, specify the application name as "main" and add a URL path to a homepage function we will write in the next step. Save the file.
Create the homepage view
env > mysite > main > views.py
from django.shortcuts import render
# Create your views here.
return render(request = request, template_name="main/home.html" )
Listed directly below the main > urls.py file is views.py. Open views.py and add the code above that states when the homepage URL is requested the home.html file will render. Be sure to save the file.
Add the main URLs to the mysite URLs
env > mysite > mysite > urls.py
"""mysite URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
from django.contrib import admin
from django.urls import path, include #add include
urlpatterns = [
path('', include ('main.urls')), #add this
Finally, we have to add the URLs from the main application to the mysite project. To do this, go to urls.py located in mysite > mysite and import include. Then add a path to include the main > urls.py file under urlpatterns.
Migrate initial changes to the database
(env)User-Macbook:mysite user$ python3 manage.py migrate
Windows Command Prompt
(env)C:\Users\Owner\desktop\env\mysite> py manage.py migrate
To ensure your web app will run correctly, apply the migrations preset by the Django web framework.
Run the development server
(env)User-Macbook:mysite user$ python3 manage.py runserver
(env)C:\Users\Owner\desktop\env\mysite> py manage.py runserver
Finally, run the development server in your CLI and go to http://127.0.0.1:8000/ to see your home.html template displaying in the browser.
Subscribe to stay current on our latest articles and promos
Post a Comment
Join the community
April 25, 2020, 2:43 p.m.
Oct. 16, 2020
Oct. 13, 2020
Oct. 6, 2020
Oct. 5, 2020
Sept. 21, 2020