\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{masterful45} \pdfinfo{ /Title (django-m1-info-gl.pdf) /Creator (Cheatography) /Author (masterful45) /Subject (Django - M1 Info - GL Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{41A344} \definecolor{LightBackground}{HTML}{F3F9F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Django - M1 Info - GL Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{masterful45} via \textcolor{DarkBackground}{\uline{cheatography.com/43480/cs/12954/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}masterful45 \\ \uline{cheatography.com/masterful45} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 28th September, 2017.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{p{1.04 cm} x{6.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Installation}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Python} & \seqsplit{https://www.python.org/downloads/} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} pip & \seqsplit{https://pip.pypa.io/en/latest/installing/} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{Django} & pip install Django \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{C'est quoi, Django?}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Django est un {\bf{framework Web}}. Parce que Django a été développé dans un environnement journalistique évoluant très rapidement, il a été conçu pour rendre le développement Web {\bf{rapide et facile}}.} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Son slogan en est la preuve : "Django: The Web framework for perfectionists with deadlines" .} \tn % Row Count 8 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Django s'inspire fortement de l'architecture MVC. On dit que Django est en {\bf{MTV}} pour {\bf{Modèles, Templates et Vues}}.} \tn % Row Count 11 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Django se {\bf{compose de modules appelés "applications"}} qui peuvent être inclus ou retirés facilement. Grossièrement , dans un site conçu avec Django, la gestion des utilisateurs est une app et la chatbox en est une autre.} \tn % Row Count 16 (+ 5) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Django est open source, distribué sous{\bf{ licence BSD 3-clauses}} et developpé et maintenu par la Django Software Foundation.} \tn % Row Count 19 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Déploiement :}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Attention}} : Django fournit un serveur web très simple pour déboguer et tester rapidement. Il ne doit jamais être utilisé en production car il n'a jamais passé d'audit de sécurité et ne supportera pas la charge de plusieurs utilisateurs.} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{git clone votreprojet} \tn % Row Count 6 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{cd votreprojet} \tn % Row Count 7 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{./manage.py check -{}-deploy} \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{./manage.py migrate} \tn % Row Count 9 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{./manage.py createsuperuser} \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Configurer le fichier WSGI} \tn % Row Count 11 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{lancer votre serveur en prod (apache, nginx etc...)} \tn % Row Count 13 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{et se rendre à la page d'acceuil de votre site !} \tn % Row Count 14 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{5.68 cm} x{2.32 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Avantages et Incovénients :}} \tn % Row 0 \SetRowColor{LightBackground} Accès à la librairie standard Python. & Pas d'AJAX. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Développement simple, efficace et rapide. & ORM \seqsplit{capricieuse}. \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Interface d'Administration et autres générés automatiquement à partir des modèles. & Evolution lente. \tn % Row Count 8 (+ 4) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{Déboguage et tests.} \tn % Row Count 9 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Communauté et Documentation.} \tn % Row Count 10 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Modèles :}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Le modèle est la seule et unique source d'information concernant les données. Il contient les attributs et le comportement des données stockées.} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Chaque modèle correspond à une seule table dans la base de données.} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Chaque modèle est une classe Python qui dérive de django.db.models.Model.} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Chaque attribut du modèle représente une colonne de la table.} \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Avec tout ça, une API d'accès aux données est automatiquement générée par le cadre compatible CRUD. Inutile d'écrire des requêtes SQL associées à des formulaires, elles sont générées automatiquement par l'ORM.} \tn % Row Count 14 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Modèle exemples :}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Dans cet exemple on définit le modèle Personne avec son prénom et son nom de famille}} \newline % Row Count 2 (+ 2) from django.db import models \newline % Row Count 3 (+ 1) class Personne(models.Model): \newline % Row Count 4 (+ 1) prenom= \seqsplit{models.CharField(max\_length=30)} \newline % Row Count 5 (+ 1) nomdefamille= \seqsplit{models.CharField(max\_length=30)} \newline % Row Count 6 (+ 1) {\bf{Le modèle définit au-dessus créera une table de ce style:}} \newline % Row Count 8 (+ 2) CREATE TABLE myapp\_personne ( \newline % Row Count 9 (+ 1) "id" SERIAL NOT NULL PRIMARY KEY, \newline % Row Count 10 (+ 1) "prenom" VARCHAR(30) NOT NULL, \newline % Row Count 11 (+ 1) "nom de famille" VARCHAR(30) NOT NULL \newline % Row Count 12 (+ 1) ); \newline % Row Count 13 (+ 1) {\bf{Les fonctions qui agissent sur le modèle doivent se trouver avec le modèle dans models.py:}} \newline % Row Count 15 (+ 2) def personne\_agee(self): \newline % Row Count 16 (+ 1) "Retourne un indicateur de vieillesse." \newline % Row Count 17 (+ 1) import datetime \newline % Row Count 18 (+ 1) if self.birth\_date \textless{} datetime.date(1945, 8, 1): \newline % Row Count 20 (+ 2) return "salut l'antiquité" \newline % Row Count 21 (+ 1) else: \newline % Row Count 22 (+ 1) return "salut la jeunesse"% Row Count 23 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{new page}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Templates:}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Etant un framework Web, Django a besoin d'une méthode pratique pour générer du HTML dynamiquement. La solution la plus commune pour ce faire viennent de l'utilisation des templates.} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Un template contient les parties statiques de la sortie HTML désirée mais aussi d'autres lignes suivant une syntaxe spécifique décrivant comment le contenu dynamique sera inséré.} \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Un projet Django peut être configuré avec une ou plusieurs moteurs de template.} \tn % Row Count 10 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Par défaut, Django utilise son propre système suivant le Django Template Language (DTL).} \tn % Row Count 12 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Un template Django est donc un fichier qui contient du texte répondant à une certaine syntaxe.} \tn % Row Count 14 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Par convention on les place dans le dossier \seqsplit{mon\_app/templates/mon\_app}} \tn % Row Count 16 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Template exemples :}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Variables}} \{\{ a \}\} \newline % Row Count 1 (+ 1) Une variable a pour sortie la valeur du contexte qui est un dictionnaire liant un ou plusieurs couples clef-valeur. \newline % Row Count 4 (+ 3) `Mon prénom est \{\{ prenom \}\}.` \newline % Row Count 5 (+ 1) {\bf{Tags}} \{\% a \%\} \newline % Row Count 6 (+ 1) `\{\% csrf\_token \%\}` \newline % Row Count 7 (+ 1) {\bf{Filtres}} \{\{ a|filtre \}\} \newline % Row Count 8 (+ 1) `\{\{ django|title \}\}` \newline % Row Count 9 (+ 1) Avec \{'django': 'the web framework for perfectionists with \newline % Row Count 11 (+ 2) deadlines'\}, cette template affiche: \newline % Row Count 12 (+ 1) The Web Framework For Perfectionists With Deadlines \newline % Row Count 14 (+ 2) {\emph{-\textgreater{} la première lettre de chaque mot devient une majuscule}} \newline % Row Count 16 (+ 2) {\bf{Un commentaire ressemble à ça :}} \newline % Row Count 17 (+ 1) `\{\# ça ne sera pas affiché à l'écran \#\}`% Row Count 18 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{ded}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Vues :}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Une fonction de vue, ou une vue pour faire court, est simplement une fonction Python qui prend une requête Web et retourne une réponse Web.} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Cette réponse peut être le contenu HTML d'une page,une redirection, une erreur 404, un document XML, une image etc...} \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{La vue contient donc la logique nécessaire pour retourner cette réponse. Par convention, on mettra les vues dans le fichier views.py .} \tn % Row Count 9 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Vue exemple :}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Voilà une vue qui retourne la date et le temps actuel sous forme d'un document HTML :}} \newline % Row Count 2 (+ 2) from django.http import HttpResponse \newline % Row Count 3 (+ 1) import datetime \newline % Row Count 4 (+ 1) def \seqsplit{current\_datetime(request):} \newline % Row Count 5 (+ 1) now = datetime.datetime.now() \newline % Row Count 6 (+ 1) html = "\textless{}html\textgreater{}\textless{}body\textgreater{}It is now \%s.\textless{}/body\textgreater{}\textless{}/html\textgreater{}" \% now \newline % Row Count 8 (+ 2) return HttpResponse(html)% Row Count 9 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Routage (URL Dispatcher) :}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Routage du projet vers l'app}}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}\seqsplit{mon\_projet/mon\_projet/urls}.py \{\{nl\}\} `\#Include \seqsplit{mon\_projet/mon\_app/urls}.py urlpatterns = patterns('', url(r'\textasciicircum{}mainApp/', include('mainApp.urls')),)`} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Routage de l'app vers la vue}}} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}\seqsplit{mon\_projet/mon\_app/urls}.py \{\{nl\}\}`from django.conf.urls import url, patterns from mainApp import views urlpatterns = patterns('', url(r'\textasciicircum{}\$', views.index, name='index'),)`} \tn % Row Count 10 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.08 cm} x{5.92 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Ressources :}} \tn % Row 0 \SetRowColor{LightBackground} Site du projet & \{\{link="https://www.djangoproject.com/"\}\}https://www.djangoproject.com\{\{/link\}\} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} Snippets & \{\{link="https://djangosnippets.org/"\}\}https://djangosnippets.org/\{\{/link\}\} \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} Apps & \{\{link="https://djangopackages.org/"\}\}https://djangopackages.org/\{\{/link\}\} \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} Docs & \{\{link="https://docs.djangoproject.com/fr/1.11/"\}\}https://docs.djangoproject.com/fr/1.11/\{\{/link\}\} \tn % Row Count 13 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}