Logo
Back to home

Python

Python Indentation
Dec 18, 2020 5.7k 110 minutes ago

A block is a group of statements that are meant to be executed together. In Python, statements are grouped using whitespace, that is, blocks are indented within other blocks instead of using curly braces. if True: # execute this block of statements print("Block 1") else: # execute other block of statements print("Block 2") If statement, without indentation (will raise an error): if True: # execute this block of statements print("Block 1") else:

Python Comments
Dec 18, 2020 5.6k 110 minutes ago

Comments are an integral part of programming. There are three different ways to write Python comments, documentation strings (docstrings for short), inline comments, and block comments. Block Comments Step 1 : Block and inline comments start with a pound sign, #. A block comment comes in the line before the statement it annotates and is placed at the same indentation level: # increment counter counter = counter + 1 Inline Comments Step 2 : Inline comments are placed on the same line as the sta

Python User Input
Dec 18, 2020 6.1k 110 minutes ago

User Input from the Keyboard Python has a very handy function for obtaining user keyboard input from the CLI called input(). When called, this function allows the user to type input into your program using their keyboard. Step 1 : Declare the following variable: message = input() The program execution will halt until you input a value and hit the Enter key Step 2 : The message variable was assigned to the value that we passed. Let's print it out: print(message) Passing in a Prompt to the input

How To Set Up Django with uWSGI, PostgreSQL and Nginx on Ubuntu 20.04
Dec 03, 2020 11.1k 110 minutes ago

We will install Django and all the components that are needed for Nginx and Django to be able to communicate. Install the Packages Step 1 : Install the Packages from the Ubuntu repositories sudo apt-get update sudo apt-get install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx Setting up Django Step 2 : Upgrade pip and install the package by typing sudo -H pip3 install --upgrade pip sudo -H pip3 install virtualenv Step 3 : Create a new Django Project mkdir /var/www/myp

Django 3 : Creating and getting session variables
Dec 03, 2020 7.0k 77 minutes ago

With Django, storage in a database, generation of the hash code, and exchanges with the client will be transparent. Sessions are stored in the context represented by the request variable. To save a value in a session variable, we must use the following syntax: request.session['session_var_name'] = "Value" Once the session variable is registered, you must use the following syntax to recover it: request.session['session_var_name'] Creating and getting session variables Step 1 : First, we must defi

Django 3 : Create a Django form
Nov 30, 2020 6.3k 110 minutes ago

Django forms work with an object that inherits from the Form class. This object will handle much of the work we have done manually in the previous post. Step 1 : Add the following URL to your urls.py file: path('create-profile-forms', views.create_profile_forms, name="create_profile_forms"), Step 2 : We will create our view in the views.py file with the following lines: from django.shortcuts import render from myproject.models import UserProfile from django.http import HttpResponse from django

Django 3 : Create an HTML form
Nov 30, 2020 5.5k 110 minutes ago

In this post, we will show you how to add a profile without using Django forms. This example will show the time that can be saved by using Django. Step 1 : Add the following URL to your urls.py file: path('create-profile', views.create_profile, name="create_profile"), Step 2 : We will create a template before the view. Indeed, we are going to fill the view with the template that contains the form. We do not put all the fields in the model because the code is too long. The following is our templ

Django 3 : Deleting objects
Nov 24, 2020 6.5k 110 minutes ago

Deleting a record Step 1To delete a record in the database, we must use the delete() method. Removing items is easier than changing items, because the method is the same for a queryset as for the instances of models. An example of this is as follows from myproject.models import UserProfile profile = UserProfile.objects.get(id = 1) profile.delete() Step 2 : The code template {% extends "base.html" %} {% block title %} {{ action }} {% endblock %} {% block h1 %} {{ action }} {% endblock %} {%

Django 3 : Updating objects
Nov 24, 2020 7.4k 110 minutes ago

Updating a model instance Step 1Updating the existing data is very simple. We have already seen what it takes to be able to do so. The following is an example where it modifies the first profile: from myproject.models import UserProfile profile = UserProfile.objects.get(id = 1) profile.name = 'Updateing profile' profile.save() Step 2 : The code template {% extends "base.html" %} {% block title %} {{ action }} {% endblock %} {% block h1 %} {{ action }} {% endblock %} {% block content %} {%

Django 3 : Creating objects
Nov 24, 2020 5.3k 110 minutes ago

Step 1 We can create the records in the UserProfile model as follows: from myproject.models import UserProfile profile = UserProfile(name='New Profile', login='newprofile') profile.save() Step 2 : The code template {% extends "base.html" %} {% block title %} {{ action }} {% endblock %} {% block h1 %} {{ action }} {% endblock %} {% block content %} {% if all_user_profile|length > 0 %} <table> <thead> <tr> <td>ID</td> <td>Na

Django 3 : Getting data from the database
Nov 24, 2020 15.4k 110 minutes ago

Before using Django to retrieve data from a database, we were using SQL queries to retrieve an object containing the result. With Django, there are two ways to retrieve records from the database depending on whether we want to get back one or several records. Getting multiple records Step 1 : To retrieve records from a model, we must first import the model into the view as we have done before to save the data in a model. We can retrieve and display all the records in the UserProfile model as fol

Django 3 : Creating an administration site for models
Nov 23, 2020 5.4k 110 minutes ago

The administration module is very convenient and is included by default with Django. It is a module that will maintain the content of the database without difficulty. This is not a database manager because it cannot maintain the structure of the database. Installing the module Step 1 : The django.contrib.admin application is already included in the INSTALLED_APPS setting, so you don't need to add it django.contrib.admin Step 2 : You also have to edit the urls.py file by adding or uncommenting t

Django 3 : Creating our first model
Nov 23, 2020 5.5k 110 minutes ago

Databases and Django Django can interface with many databases. However, during the development of our application, we use SQLite libraries that are included in Django. Step 1 : We will modify settings.py to set our connection to the database: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } Creating simple models Step 2 : First, you need to define a UserProfile model. Add the following lines to the models.py in the m

Django 3 : Using static files in templates
Nov 20, 2020 6.8k 110 minutes ago

Static files such as JavaScript files, CSS, or images are essential to obtain an ergonomic website. These files are often stored in a folder, but they can be useful to modify this folder under development or in production. Step 1 : Django allows us to define a folder containing the static files and to easily modify its location when required. To set the path where Django will look for static files, we have to change our settings.py file by adding or changing the following line: STATIC_URL = '/st

Django 3 : Extending the templates
Nov 20, 2020 5.9k 110 minutes ago

The legacy of templates allows you to define a super template and a subtemplate that inherits from the super template. In the super template, it is possible to define blocks that subtemplates can fill. We will use an example where the page.html template will extend the base.html template. Step 1 : The following is the base.html template code, which we must create in the template folder: <html> <head> <title> {% block title %}{% endblock %} </title&gt