Raza Rython 📜 Profile picture
Head of Growth @Scroll_ZKP • Scroll Everyone Everywhere

May 14, 2021, 22 tweets

Beginning a thread🧵 about the basics in #Python you need to know for #Django👇

Django is a #webframework to build interactive #websites for Python and is great for fast #development

1/ 19 Variables are used to store information, so that they can be used later on in your code.
In Python a ‘string’ is text and a ‘integer’ or ‘float’ is a number
‘car_brand’ and ‘brand_type’ are variables in the example below

Variables can be stored inside other variables

2/19
No special signs are allowed to be used in the name of variables, except for ‘_’ like.
Numbers are allowed in the name of a variable, but not at the start
So ‘1car’ is not OK and ‘car1’ is OK.

3/19
Lists

Lists store multiple pieces of information in a specific order.
A list always starts and ends with a square bracket ‘[‘ ‘]’.
Python starts counting at 0.
So the position or index number of the first item in the list is 0.
to print the first item, use #Code1

We can add items to the list by using the ‘append’ function.
See #Code2
A ‘for loop’ or looping allows you to perform the same action for multiple items in a list or group of items

4/19
A Tuple contains multiple items like a list.
The big difference is that a Tuple is immutable.
Which basically means you can’t change the values in a tuple
Unlike a list which is mutable or changeable

5/19
A dictionary is similar to a list.
It contains a set of item, but each item is associated with a key.
See below
The part before the colon is the ‘key’ and the part after the colon is the ‘value’
Together they are called a ‘key-value pair’.

6/19
The if statement is used in python when the program has to consider conditions
And reply in a certain way depending on the condition

7/19
While loops

The while loop continues the loop as long as a condition is valid
This code will continue to print the variable number
Adding 1 to the value in every iteration
Until the value 5 is reached

8/19
Functions
A function is a block of code that is written for a specific job
The function can be called by using the name of the function
Here’s an example

9/19

Functions part two

A keyword argument is a name-value pair, which directly links the argument to the parameter inside a function allowing to ignore the order inside a function.

10/19

Classes

A class defines the type of information an object can store and what functions it can do
Here’s an example

11/19

A class can use attributes and methods from another class. This is called inheritance.

12/19
BASIC DJANGO - now let's get into django

Best practice is to install django in a virtual environment
A virtual environment is an isolated environment where you can develop safely without

13/19

Create a project
To start with django you need to create a project, app, database and server

14/19

Include the app in the settings.py file inside your project.
Django doesn’t know our app exists yet, this way we tell django that there is an app called ‘myfirstapp’

15/19 Django is structured as follows.
When a user types audi.com/about.

This is what happens:
Urls.py -> views.py -> Model(if there is one) -> template
The urls.py contains the location of the page

the views.py contains the content of the page
The models.py contains the data of the page
The template contains the structure and styling of the page

Create the content of your page in the views.py file inside your app

16/19
Create an ‘urls.py’ file inside your app and configure the links inside the app

17/19
Create a ‘templates’ folder inside your app. Create a home.html file inside your text editor with the following html tag: <h1> Hello </h1>. Put the file ‘home.html’ inside your templates folder.

18/19
This time go to the urls.py file in your project, not your app and include a reference to the urls.py file of your app. Don’t forget to write ‘include’ after ‘path’ on the import line.

19/19
you have learned how to create a basic django structure with one webpage

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling