0% found this document useful (0 votes)
42 views7 pages

Ch-11 Getting Started With DJango

Uploaded by

Sujeet Kale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views7 pages

Ch-11 Getting Started With DJango

Uploaded by

Sujeet Kale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter- 11.

Getting Started with DJango


11.1 About 3 core files : model.py, urls.py, views.py
11.2 Setting up database connections (MySQL/SQLServer)
11.3 Managing Users & DJango admin tools
11.4 Installing and using ‘Out of the Box’ Django features

About 3 core files : model.py, urls.py, views.py


There are 3 core files of Django framework which are used to develop any web
application.
1. Models.py
2. Urls.py
3. Views.py

1. models.py:
● A model is a class that represents a table or collection in our DB, and
where every attribute of the class is a field of the table or collection.
● Models are defined in the app/models.py
● This file can contain multiple models
● Every model inherits from django.db.models.Model.
● In short, Django Models is the SQL of Database one uses with Django.
● SQL (Structured Query Language) is complex and involves a lot of
different queries for creating, deleting, updating or any other stuff
related to databases.
● Django models simplify the tasks and organize tables into models.
Generally, each model maps to a single database table.
Basics of a model include –

● Each model is a Python class that subclasses


django.db.models.Model.
● Each attribute of the model represents a database field.
● With all of this, Django gives you an automatically-generated
database-access API.
To use Django Models first create a Django Project and Django app in it.

Create Table (Model)


To create a new table, we must create a new model.

Syntax

from django.db import models

class ModelName(models.Model):

field_name = models.Field(**options)
Open the models.py file. It is almost empty by default, with only an import
statement and a comment:

models.py:
from django.db import models

# Create your models here.

To add a Members table in our database, start by creating a Members class, and
describe the table fields in it:

from django.db import models

class Members(models.Model):
username = models.CharField(max_length=255)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
email = models.CharField(max_length=255)
Whenever we are creating, deleting or updating a model, 2 commands should
be executed:
Makemigrations: generates SQL command for already installed apps and
nelly created apps
Python manage.py makemigrations

Migrate: executes these commands in the database file.


Python manage.py migrate
After we have created a model we can perform various operations such as
creating a Row for the table or in terms of Django Creating an instance of Model

Django lets us interact with its database models, i.e. add, delete, modify and
query objects, using a database-abstraction API called ORM(Object Relational
Mapper). We can access the Django ORM by running the following command
inside our project directory.
Step 1. python manage.py shell
Now the Python interpreter (Python shell) is running.
(InteractiveConsole)
>>>

Step 2. At the bottom, after the three >>> write the following
>>> from Members.models import Members

Step 3: type Members.objects.all()


After pressing Enter, it shows empty Members table:<QuerySet []>
This should give you an empty QuerySet object. A QuerySet is a collection of data
from a database.

Step 4: Add record to the table by typing


>>> member = Members(username=’raj22’,
firstname='Raj',
lastname='Patil',
email=’[email protected]’ )
>>> member.save()

Step 5: Execute this command to see if the Members table got a member:
>>> Members.objects.all().values()
Result will be:
<QuerySet [{'id': 1, ‘username’: ‘raj22’, 'firstname': 'Raj',
'lastname': 'Patil', ‘email’: ‘[email protected]’}]>

View in Browser
To see the result in a web page, we can create a view.open the views.py file
from django.http import HttpResponse
from django.template import loader
from .models import Members

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

return HttpResponse(output)

Navigate to the project folder and type this to start the server:

py manage.py runserver

Render a model in Django Admin Interface

To render a model in Django admin, we need to modify app/admin.py.


Go to admin.py in the app and enter the following code. Import the corresponding
model from models.py and register it to the admin interface.
from django.contrib import admin
# Register your models here.
from .models import <<Model_Name>>
admin.site.register(<<Model_Name>>
)
Now we can check whether the model has been rendered in Django Admin.
Django Admin Interface can be used to graphically implement CRUD (Create,
Retrieve, Update, Delete).

11.3 Managing Users & DJango admin tools

● One of the most powerful parts of Django is the automatic admin interface.
● It reads metadata from your models to provide a quick, model-centric
interface where trusted users can manage content on your site.
● Django comes with a built-in authentication system.
● The authentication system includes users, groups, and permissions.
● When a model is created, Django will automatically create four default
permissions for the following actions:
1. add: Users with this permission can add an instance of the model.
2. delete: Users with this permission can delete an instance of the
model.
3. change: Users with this permission can update an instance of the
model.
4. view: Users with this permission can view instances of this model.
● To create an admin user type below command:
○ python manage.py createsuperuser
○ Username (leave blank to use 'admin'): reshma
○ Email address: [email protected]
○ Password:
○ Password (again):
○ Superuser created successfully.
● Run your server in the background in bash by command python manage.py
runserver. Head over to the browser and type the following in the URL.
● http://127.0.0.1:8000/admin
● Fill out your details afterward, i.e., the username and password that you've
created earlier:

View your admin panel afterward with our newly created models ''Students”.

Change the content of the 'Students’' by clicking the 'Add' button. Fill out the
information and 'Save' the details.
Edit or Delete the Student by clicking on a particular student object.

ManagingMultiple Applications Inside


Django Project.
https://medium.com/@aman_adastra/day-8-of-100-days-of-django-multiple-
applications-inside-django-project-3bf580760745

You might also like