Book Image

Django Essentials

By : Samuel Dauzon
Book Image

Django Essentials

By: Samuel Dauzon

Overview of this book

<p>Django is a powerful Python web framework designed for rapid web application development. With the advent of frameworks such as Django, web developers have been forced to adopt MVC architectures and are encouraged to develop quality code. This quality allows several developers to work together easily and reduces the number of bugs due to human errors.</p> <p>Beginning with the basics of the Web and Django, the book explains the MVC pattern. It then moves on to explain the step-by-step installation of Python, PIP, and Django on Windows, Linux, and Mac OS. Furthermore, you will learn how to create templates, models, forms, and so on. After reading the book, you will be able to quickly create dynamic web applications with AJAX and an admin part.</p> <p>This book features a step-by-step approach that shows you how to program, create, and improve the quality of web applications using Django, with the help of Python.</p>
Table of Contents (20 chapters)
Django Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The template language


When a developer develops templates, he/she regularly needs to use the template language and filters.

Template tags

The following are the key elements of the template language:

  • {% autoescape on OR off %} {% endautoescape %}: This automatically starts the auto-escape feature that helps protect the browser of the displayed data (XSS).

  • {% block block_name %} {% endblock %}: This sets the blocks that can be filled by templates that inherit from them.

  • {% comment %} {% endcomment %}: This sets a comment that will not be sent to the user as HTML.

  • {% extends template_name %}: This overrides a template.

  • {% spaceless %}: This removes all the whitespaces between the HTML tags.

  • {% include template_name %}: This includes a template named template_name in the current template. The blocks included templates that cannot be redefined.

Loops in dictionaries

This section shows you how to loop through a dictionary. The steps involved in looping are as follows:

  • {% for var in list_var %}: This allows looping in the list_var dictionary

  • {% empty %}: This displays the subsequent code if the dictionary is empty

  • {% endfor %}: This indicates the end of a loop

Conditional statements

This section shows how to execute a conditional statement:

  • {% if cond %}: This line checks the condition and discusses the following code when enabled.

  • {% elif cond %}: This line checks another condition if the first has not been verified. If this condition is satisfied, the following code will be processed.

  • {% else %}: This line will process the following code if none of the previous conditions have been validated.

  • {% endif %}: This line ends the processing of conditions.

The template filters

The following are the different template filters:

  • addslashes: This adds slashes before quotes

  • capfirst: This capitalizes the first character

  • lower: This converts the text into lowercase

  • upper: This converts the text into uppercase

  • title: This capitalizes all the first characters of each word

  • cut: This removes all the values of the argument from the given string, for example, {{ value|cut:"*" }} removes all the * characters

  • linebreaks: This replaces line breaks in text with the appropriate HTML tags

  • date: This displays a formatted date, for example, {{ value|date:"D d M Y" }} will display Wed 09 Jan 2008

  • pluralize: This allows you to display plurals, shown as follows:

    You have {{ nb_products }} product{{ nb_products|pluralize }} in our cart.
    I received {{ nb_diaries }} diar{{ nb_diaries|pluralize:"y,ies" }}.
  • random: This returns a random element from the list

  • linenumbers: This displays text with line numbers at the left-hand side

  • first: This displays the first item in the list

  • last: This displays the last item in the list

  • safe: This sets a non-escape value

  • escape: This escapes an HTML string

  • escapejs: This escapes characters to use in JavaScript strings

  • default: This defines a default value if the original value equals None or empty; for example, with {{ value|default:"nothing" }}, if the value is "", it will display nothing.

  • dictsort: This sorts the dictionary in the ascending order of the key; for example, {{ value|dictsort:"price"}} will sort the dictionary by price

  • dictsortreversed: This is used to sort the dictionary in the descending order of the key

  • floatformat: This formats a float value, and the following are the examples:

    • When 45.332 is the value,{{ value|floatformat:2 }} displays 45.33

    • When 45.00 is the value,{{ value|floatformat:"-2" }} displays 45

The queryset methods

The following are the queryset methods:

  • all(): This method retrieves all the records of a model.

  • filter(condition): This method allows you to filter a queryset.

  • none(): This method can return an empty queryset. This method is useful when you want to empty a queryset.

  • dinstinct(field_name): This method is used to retrieve the unique values of a field.

  • values_list(field_name): This method is used to retrieve the data dictionary of a field.

  • get(condition): This method is used to retrieve a record from a model. When using this method, you must be sure that it concerns only one record.

  • exclude(condition): This method allows you to exclude some records.

The following elements are the aggregation methods:

  • Count(): This counts the number of records returned

  • Sum(): This adds the values in a field

  • Max(): This retrieves the maximum value of a field

  • Min(): This retrieves the minimum value of a field

  • Avg(): This uses an average value of a field