Django: Building a Page Cheat Sheet

Users interact with a project through web pages, and a project’s home page can start out as a simple page with no data. A page usually needs a URL, a view, and a template.

Mapping a project’s URLs

The project’s main file tells Django where to find the
files associated with each app in the project.

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(,
    url(r'', include('learning_logs.urls', namespace='learning_logs')),

Mapping an app’s URLs

An app’s file tells Django which view to use for each URL in
the app. You’ll need to make this file yourself, and save it in the
app’s folder.

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),

Writing a simple view

A view takes information from a request and sends data to the browser, often through a template. View functions are stored in an app’s file. This simple view function doesn’t pull in any data, but it uses the template index.html to render the home page. from django.shortcuts import render

def index(request):
    """The home page for Learning Log."""
    return render(request, 'learning_logs/index.html')

Writing a simple template

A template sets up the structure for a page. It’s a mix of html and template code, which is like Python but not as powerful. Make a folder called templates inside the project folder. Inside the templates folder make another folder with the same name as the app. This is where the template files should be saved.

<p>Learning Log</p>
<p>Learning Log helps you keep track of your
learning, for any topic you're learning

Leave a Reply

Your email address will not be published. Required fields are marked *