Open source softwares - Django

Back to Course

Lesson Description

Lession - #996 Django-Add Members

Add Records

The Members table is empty, we ought to add a few individuals to it.

In the following sections you will figure out how to make a UI that will deal with CRUD tasks (Create, Read, Update, Delete>
, yet for the time being, we should compose Python code directly in the Python mediator (Python shell>
and add a few individuals in our data set, without the UI.

To open a Python shell, type this command:

py manage.py shell
Presently we are in the shell, the outcome ought to be something like this:

Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55>
[MSC v.1928 64 bit (AMD64>
] on win32 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole>
At the bottom, after the three >>> write the following:

>>> from members.models import Members
Hit [enter] and compose this to look at the unfilled Members table:

>>> Members.objects.all(>
This should give you a empty QuerySet object, similar to this:

<QuerySet []>
A QuerySet is an collection of information from a data set.

Add a record to the table, by executing these two lines:

>>> member = Members(firstname='Emil', lastname='Refsnes'>
>>> member.save(>

Execute this command to check whether the Members table got a member:

>>> Members.objects.all(>
Ideally, the outcome will seem to be this:

<QuerySet [{'id': 1, 'firstname': 'Emil', 'lastname': 'Refsnes'}]<

Add Multiple Records

You can add various records by making a list of Members obejcts, and execute .save(>
on every entry:

>>> member1 = Members(firstname='Tobias', lastname='Refsnes'>
>>> member2 = Members(firstname='Linus', lastname='Refsnes'>
>>> member3 = Members(firstname='Lene', lastname='Refsnes'>
>>> member4 = Members(firstname='Stale', lastname='Refsnes'>
>>> members_list = [member1, member2, member3, member4] >>> for x in members_list: >>> x.save(>
Now there are 5 members in the Members table:

>>> Members.objects.all(>
<QuerySet [{'id': 1, 'firstname': 'Emil', 'lastname': 'Refsnes'}, {'id': 2, 'firstname': 'Tobias', 'lastname': 'Refsnes'}, {'id': 3, 'firstname': 'Linus', 'lastname': 'Refsnes'}, {'id': 4, 'firstname': 'Lene', 'lastname': 'Refsnes'}, {'id': 5, 'firstname': 'Stale', 'lastname': 'Refsnes'}]>

View in Browser

We need to see the outcome in a page, not in a Python shell environment.

To see the outcome in a page, we can make a view for this specific task.

In the individuals app, open the views.py document, assuming you have followed the past sections of this instructional exercise, it ought to seem to be this:

from django.http import HttpResponse
from django.template import loader

def index(request>
: template = loader.get_template('myfirst.html'>
Change the substance in the views.py document to seem to be this all things being equal:

from django.http import HttpResponse
from django.template import loader
from .models import Members

def index(request>
: mymembers = Members.objects.all(>
output = "" for x in mymembers: output += x["firstname"] return HttpResponse(output>

As you can find in line 3, the Members model is imported, and the index view does the accompanying:

  • makes a mymembers object with every one of the values of the Members model.
  • Loops through all things in the mymembers object to build a string with all the firstname values.
  • Returns the string as result to the program. See the outcome in your program. Assuming that you are still in the Python shell, compose this command to leave the shell:

    >>> quit(>

    Explore to the/myworld/folder and type this to begin the server:

    py manage.py runserver