Posts

Django 3 : Creating and getting session variables

Louis SanchezApril 18th 2021, 4:49
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...
397

Django 3 : Create a Django form

Louis SanchezApril 18th 2021, 6:28
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...
443

Django 3 : Create an HTML form

Louis SanchezApril 18th 2021, 6:23
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...
436

Django 3 : Deleting objects

Louis SanchezApril 18th 2021, 5:03
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...
298

Django 3 : Updating objects

Louis SanchezApril 18th 2021, 5:57
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()...
306

Django 3 : Creating objects

Louis SanchezApril 18th 2021, 5:54
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 }}...
303

Django 3 : Getting data from the database

Louis SanchezApril 18th 2021, 6:33
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...
561

Django 3 : Creating an administration site for models

Louis SanchezApril 18th 2021, 6:34
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...
334

Django 3 : Creating our first model

Louis SanchezApril 18th 2021, 5:00
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',...
278

Django 3 : Using static files in templates

Louis SanchezApril 17th 2021, 7:37
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...
358