\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{dbean} \pdfinfo{ /Title (django-basics.pdf) /Creator (Cheatography) /Author (dbean) /Subject (django basics 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}{1F1F1E} \definecolor{LightBackground}{HTML}{F8F8F7} \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 basics Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{dbean} via \textcolor{DarkBackground}{\uline{cheatography.com/128720/cs/25468/}}} \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}dbean \\ \uline{cheatography.com/dbean} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 1st January, 2021.\\ Updated 3rd January, 2021.\\ 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}{x{3.92 cm} x{4.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{setup}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{creating a project}} & `django-admin startproject mysite .` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} changing settings & edit mysite/settings.py \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} database setup & `python3 manage.py migrate` \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} run the server & `python3 manage.py runserver` \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} and in browser: & \seqsplit{http://127.0.0.1:8000/} \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} test shit in an interactive console & `python3 manage.py shell` \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.16 cm} x{3.84 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{apps}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{creating an application}} & `python3 manage.py startapp blog` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} add the app name to `INSTALLED\_APPS` & \seqsplit{`'blog.apps.BlogConfig'`} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{models are kept in models.py} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{add, edit and delete model in admin.py} \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{django querysets}} \tn % Row 0 \SetRowColor{LightBackground} getting the model classes & `from {\emph{app\_name}}.models import {\emph{model\_name}}` \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} getting the user model & `from \seqsplit{django.contrib.auth.models} import User` \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} see all objects of a certain model & \seqsplit{`Class\_name.objects.all()`} (is iterable) \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} create an object & \seqsplit{`Class\_name.objects.create(attributes)`} you don't have to save for it to be reflected \tn % Row Count 13 (+ 5) % Row 4 \SetRowColor{LightBackground} accessing the object via attribute & \seqsplit{`Class\_name.objects.get(attr="value")`} \tn % Row Count 15 (+ 2) % Row 5 \SetRowColor{white} filtering (can return multiple, iterable) & \seqsplit{`Class\_name.objects.filter(attr="value")`} \tn % Row Count 18 (+ 3) % Row 6 \SetRowColor{LightBackground} filtering (contains) & \seqsplit{`Class\_name.objects.filter(title\_\_contains="phrase")`} \tn % Row Count 21 (+ 3) % Row 7 \SetRowColor{white} & \seqsplit{`published\_date\_\_lte=timezone}.now()` \tn % Row Count 23 (+ 2) % Row 8 \SetRowColor{LightBackground} ordering & \seqsplit{`Class\_name.objects.order\_by("attribute")`} "-attr" does the reverse \tn % Row Count 27 (+ 4) % Row 9 \SetRowColor{white} you can combine ordering and filtering & \seqsplit{`Class\_name.objects.does} this first.does this second` \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{django querysets (cont)}} \tn % Row 10 \SetRowColor{LightBackground} delete & `{\emph{obj}}.delete()` \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{1.2 cm} x{6.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{forms}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{import} & `from django import forms` \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} & `from .models import {\emph{model\_name}}` \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.16 cm} x{5.84 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{security}} \tn % Row 0 \SetRowColor{LightBackground} login decorator & `from \seqsplit{django.contrib.auth.decorators} import login\_required` \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} & `@login\_required` \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.672 cm} x{3.116 cm} x{2.812 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{model attributes}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{model attribute}} & {\bf{description}} & {\bf{args}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{`models.CharField`} & text with a limited number of characters & `max\_length` \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \seqsplit{`models.TextField`} & text field with unlimited content & \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} \seqsplit{`models.DateTimeField`} & datetime field & `default, blank, null, \seqsplit{auto\_now\_add`} \tn % Row Count 12 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{databases}} \tn % Row 0 \SetRowColor{LightBackground} creating a database & `python3 manage.py migrate` \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} update changes to database & `python3 manage.py makemigrations {\emph{app\_name}}` \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} migrate & `python3 manage.py migrate {\emph{app\_name}}` \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} register database in {\emph{app\_name}}/admin.py & `admin.site.register({\emph{Model\_name}})` \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.48 cm} x{5.52 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{admin}} \tn % Row 0 \SetRowColor{LightBackground} register a model & \seqsplit{`admin.site.register(Post)`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} create superuser & `python3 manage.py createsuperuser` \tn % Row Count 4 (+ 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}{pythonanywhere deployment}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{1. create the api token} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{2. new bash console} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{3. in bash: `pip3 install -{}-user pythonanywhere`} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{4. \seqsplit{`pa\_autoconfigure\_django}.py -{}-python=3.8 {\emph{repo\_link.git}}`} \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{5. to update website, go to your directory and pull code} \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{6. reload at web} \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{7. `workon {\emph{your\_directory}}`} \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{8. `python3 manage.py collectstatic`} \tn % Row Count 10 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.2 cm} x{4.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{basic styling}} \tn % Row 0 \SetRowColor{LightBackground} get bootstrap & `\textless{}link rel="stylesheet" \seqsplit{href="//maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min}.css"\textgreater{}` \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} & `\textless{}link rel="stylesheet" \seqsplit{href="//maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min}.css"\textgreater{}` \tn % Row Count 9 (+ 5) % Row 2 \SetRowColor{LightBackground} add static files & make a {\emph{static}} folder with css files inside {\emph{app\_name}} folder \tn % Row Count 12 (+ 3) % Row 3 \SetRowColor{white} & `\{\% load static \%\}` \tn % Row Count 13 (+ 1) % Row 4 \SetRowColor{LightBackground} & `\textless{}link rel="stylesheet" href="\{\% static 'css/blog.css' \%\}"\textgreater{}` \tn % Row Count 16 (+ 3) % Row 5 \SetRowColor{white} create a block & `\{\% block content \%\}` \tn % Row Count 17 (+ 1) % Row 6 \SetRowColor{LightBackground} & `\{\% endblock \%\}` \tn % Row Count 18 (+ 1) % Row 7 \SetRowColor{white} block content & `\{\% block content \%\}` \tn % Row Count 19 (+ 1) % Row 8 \SetRowColor{LightBackground} & `\{\% endblock \%\}` \tn % Row Count 20 (+ 1) % Row 9 \SetRowColor{white} connect a block to the base & `\{\% extends 'blog/base.html' \%\}` \tn % Row Count 22 (+ 2) % Row 10 \SetRowColor{LightBackground} link a url within a site & `a href="\{\% url '{\emph{url\_name}}' pk=post.pk \%\}"` \tn % Row Count 24 (+ 2) % Row 11 \SetRowColor{white} & where pk stands for primary key \tn % Row Count 26 (+ 2) % Row 12 \SetRowColor{LightBackground} to use the link & `path('{\emph{whatever}}/\textless{}int:pk\textgreater{}/', views.{\emph{view\_name}}, name='*url\_name')` \tn % Row Count 29 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}