01 - Project setup Flashcards
create new django project
django-admin startproject monthly_challenges
create a django app
python3 manage.py startapp challenges
run django server
python3 manage.py runserver
what are views
the logic that is executed for different URLs, code handles requests and return responses
what are URLs
Route mappings to specific action that ensure specific result when specific URL is accessed
sample index action
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse(“This works!”)
sample url to index action
from django.urls import path
from . import views
urlpatterns = [
path(“january”, views.index)
]
template folder structure
/challenges/templates/challenges/challenge.html
render a template from challenges app
def monthly_challenge(request, month):
try:
challenge_text = monthly_challenges[month]
return render(request, “challenges/challenge.html”)
except:
return HttpResponseNotFound(“<h1>This month is not supported</h1>”)
passing values from view action to template
from django.shortcuts import render
def monthly_challenge(request, month): try: challenge_text = monthly_challenges[month] return render(request, "challenges/challenge.html", { "text": challenge_text }) except: return HttpResponseNotFound("<h1>This month is not supported</h1>") //challenges/templates/challenges/challenge.html <body> <h1>This month's Challenge</h1> <h2>{{text}}</h2> </body>
adding filters to view files
//challenges/templates/challenges/challenge.html
<body> <h1>{{month_name | title}</h1> <h2>{{text}}</h2> </body>
using the for tag
<ul>
{% for month in months %}
<li><a></a></li>
{% endfor %}
</ul>
using the URL tag
//index.html
<ul>
{% for month in months %}
<li><a>{{ month| title}}</a></li>
{% endfor %}
</ul>
//index.html
<ul>
{% for month in months %}
<li><a>{{ month| title}}</a></li>
{% endfor %}
</ul>
using if for conditional rendering
<body>
<h1>{{ month_name|title }} Challenge</h1>
{% if text is not None %}
<h2>{{ text }}</h2>
{% else %}
<p>There is no challenge for this month</p>
{% endif %}
</body>
using template inheritance
/templates/layout.html | base.html
<head>
<title>{% block page_title %} my challenges {% endblock %}</title>
</head>
…
<body>
{% block content %} {% endblock%}
</body>
//index.html
{% extends “base.html”%}
//monthly_challenges/settings.py
TEMPLATES = [
BASE-DIR / “templates”
]
including partials
/templates/challenges/includes/header.html
//challenge.html
{% include “challenges/includes/header.html” %}
sample 404 template
monthly_challenges/templates/404.html
{% extends “base.html” %}
{% block page_title %}
Something went wrong - we could not find that page!
{% endblock %}
//challenges/views.py
from django.template.loader import render_to_string
render_to_string("404.html")
//challenges/views.py
from django.http import Http404
raise Http404() //looks for 404 template
//settings.py
DEBUG = False
adding static files
/challenges/static
/challenge/challenges.css
/settings.py
INSTALLED_APPS = [
]
STATIC_URL = ‘/static/’
//inex.html
{% load static %}