Logo
Back to home

Django 3

How To Set Up Django with uWSGI, PostgreSQL and Nginx on Ubuntu 20.04
Dec 03, 2020 11.1k 116 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 116 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 116 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 116 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 116 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.5k 116 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 116 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 116 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 116 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 116 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 116 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 116 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

Django 3 : Passing Variables to the template
Nov 20, 2020 8.5k 116 minutes ago

Django 3 : Passing Variables to the template Injecting the data from the view to the template Before improving our template, we must send variables to the templates. The injection of the data is based on these variables, as the template will perform certain actions Step 1 : We will change our view to inject variables in our template. The following is our modified view: Integrating variables in templates Step 2 : In our controller, we send a variable named my_str. We can display it in a <span

Django 3 : Displaying Hello world! in a template
Nov 17, 2020 6.6k 116 minutes ago

Templates are files that will allow us to generate the HTML code returned to the client. Step 1 : We will create the first template of our application. To do so, we must first edit the settings.py file to define the folder that will contain our templates. import os # ... 'DIRS': [os.path.join(BASE_DIR, 'myproject', 'templates')], Step 2 : Now that Django knows where to look for the templates, we will create the first template of the application. To do this, use a file browser and add the index

Django 3 : Routing and Creating our first view
Nov 17, 2020 5.5k 116 minutes ago

Routing in Django Step 1 : In the previous post, we edited the settings.py file to configure our Django project. ROOT_URLCONF = 'myproject.urls' This parameter will define the Python file that will contain all the URLs of our site. When the controller receives the client request, it will go in the urls.py file and check whether the URL is a customer's request and use the corresponding view. Step 2 : This is what the urls.py file looks like, as it is created by Django when creating the pro