0% found this document useful (0 votes)
49 views

Django Record

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)
49 views

Django Record

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/ 31

MODULE-1

1) Create Django environment setup and installation in windows/Linux


Django is a Python framework that makes it easier to create web sites using
Python.Django takes care of the difficult stuff so that you can concentrate on
building your web applications.Django emphasizes reusability of components,
also referred to as DRY (Don't Repeat Yourself), and comes with ready-to-use
features like login system, database connection and CRUD operations (Create
Read Update Delete).

 First go to search bar and search CMD


 Move E drive by using command e: <enter>
 Then we get E:\>
 Create a folder by using cmd : mkdir Django_5g6 <press enter>
 Move to folder using cmd: cd Django_5g6 <press enter>
 Output: E:\Django_5g6>>
 Installation of django: pip install django

1
2) Create DJANGO project and app structure with django-admin commands

To know commands of django use cmd : >django admin

Output:

These are the commands available in django

Steps to create project:

E:/Django_5g6>django-admin startproject employeeproject <press enter>

2
Steps for creating app inside project:

E:\Django_5g6> cd employeeproject <press enter>

E:\Django_5g6>/employeeproject>django-admin startapp employees <press enter>

GO TO SUBLIME TEXT (OR) VISUAL STUDIO CODE:

E:\Django_5g6\employeeproject>code . <Press enter>

3
3) Deployment of project in server
Steps for deployment

Using cmd: E:\Django_5g6\employeeproject> python manage.py runserver

4
<Press enter>

Using: http://127.0.0.1:8000/ run server in chrome

OUTPUT:

5
MODULE-2
1)Create template in Django Project to process user interface
Step 1: connect to drive >E:

Step 2: create project


E:\Django_5g6>django-admin startproject employeeproject <press enter>

6
Step 3: connect to project

E:\Django_5g6>cd employeeproject <press enter>

Step 4: creating app inside project

E:\Django_5g6>django-admin startproject employee <press enter>

Step 5: open folder in sublime text or visual studio

E:\Django_5g6>code . (in visual studio)

(visual studio)

7
Step 6: create template inside employee

Click on employee new ->new folder-> name it as Templates

Step 7:In emp go to settings.py-

Add import os

Add INSTALLED_APPS = [

'employee’,]

8
Step 8: In employee->templates->create home.html(new file)

<!DOCTYPE html>
{% load static %}
<html>
<head>
<title>
home page
</title>
</head>
<body>
<center>
<h1>home page of employeee</h1>
<img src="{%static '/images/1.jpg' %}"></img>
</center>

9
</body>
</html>

Step 9: In employee->templates->create contacts.html(new file)

<!DOCTYPE html>
{% load static %}
<html>
<head>
<title>
contact us page
</title>
</head>
<body>
<center>
<h1>contact us page of employeee</h1>
<img src="{%static '/images/2.jpg' %}"></img>

10
</center>

</body>
</html>

Step 10: In employee->templates->create products.html(new file)


<!DOCTYPE html>
{% load static %}
<html>
<head>
<title>
product page
</title>
</head>
<center>
<h1>products list page of employeee</h1>
<img src="{%static '/images/3.jpg' %}"></img>
</center>

</html>

11
Step 11:
def hello(request,number):
text="<h1>welcome the number is %s"%number
return HttpResponse(text)

12
Step 12: employee->views.py Add code
from django.shortcuts import render

# Create your views here.


from django.http import HttpResponse
from django.template import loader
def home(request):
template=loader.get_template('home.html')
return HttpResponse(template.render())
def about(request):
template=loader.get_template('about.html')
return HttpResponse(template.render())
def products(request):
template=loader.get_template('products.html')
return HttpResponse(template.render())
def contacts(request):
template=loader.get_template('contacts.html')
return HttpResponse(template.render())
def hello(request,number):
text="<h1>welcome the number is %s"%number
return HttpResponse(text)

Step 13: In employee create urls.py (new file)


from django.urls import path
from . import views
urlpatterns = [
path('home/',views.home,name='home page'),
path('about/',views.about,name='about us page'),
path('products/',views.products,name='products page'),
path('contacts/',views.contacts,name='contacts page'),
path('hello/<number>',views.hello,name='')

<SAVE CHANGES>

Step 14: In employeeproject->urls.py Add code

13
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
path('', include('employees.urls')),
path('admin/', admin.site.urls),
]

<SAVE CHANGES>

Step 15: Go cmd for running server

E:\Django_5g6\employeeproject>python manage.py runserver <press enter>

Copy http://127.0.0.1:8000/

Goto chrome and run server


http://127.0.0.1:8000/home/
We get output as above

2) Create multiple routes from using Django URL’s.

To create a new URL route in Django, you'll need to edit the urls.py file in your
app's directory. This file contains a list of URL patterns that the Django router uses
to match incoming requests to view functions. (for same project as in module-2.1)

Step 1:Create Django Project: Use the django-admin startproject employeeproject


command to create a new Django project.

Step 2:Create Django App: Inside your Django project, create a new Django app
using the command django-admin startapp employee .

Step 3:Define Views: In your Django app, define views in the views.py file. Views
are Python functions or classes that handle incoming requests and return responses.

14
Step 4:Define URL Patterns: In your Django app, define URL patterns in the
urls.py file. URL patterns map specific URLs to views in your Django project.

Step 5: Import Views: Import the views you defined in the views.py file into the
urls.py file.

Step 6: Define URL Patterns: Inside the urls.py file, define URL patterns using the
path() function or re_path() function. Each URL pattern should map a URL path to
a specific view.

Step 7:Include App URLs in Project URLs: In your project's urls.py file, include
the URLs of your app using the include() function. This allows Django to route
requests to the appropriate app.

Step 8:Test URLs: Run your Django project and navigate to the defined URLs
(e.g., /, /about/, /contact/). Django will route the requests to the appropriate views
and return the corresponding responses.

Step 9:

Save changes and runserver >

python manage.py run server <press enter>

http://127.0.0.1:8000/

http://127.0.0.1:8000/home/

http://127.0.0.1:8000/about/

http://127.0.0.1:8000/products/

http://127.0.0.1:8000/contacts/

By following these steps, you can create multiple routes using Django URLs in
your Django project. <<same as module-2.1>>

15
3)Implement template inheritance with views and images.
To load images, we have to create static app

Step 1: In employee we have to create new folder named static.

Step 2: in static folder we have to add images folder.

Step 3: In images folder we have to add a .png file.

Step 4: in settings.py we have to load static and images

STATIC_URL = 'static/'
MEDIA_URL='/images/'

STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]

Step 5: In home.html add code for image


<!DOCTYPE html>
{% load static %}
<html>
<head>
<title>
home page
</title>
</head>
<body>
<center>
<h1>home page of employeee</h1>
<img src="{%static '/images/1.jpg' %}"></img>
</center>

16
</body>
</html>

Step 6: save changes

Step 7: runserver (http://127.0.0.1:8000/home/ )

MODULE-3
1) Create a Django model named customer having fields name, age,
phone number, address and print the customer details in web page.
Here's an example of how you can create a Django model named Customer with
fields name, age, phone_number, and address, and then print the customer details
on a web page.

1.Set up your Django project and app: Create a new Django project:

> django-admin startproject myproject

2.Create a new Django app within the project:

17
>django-admin startapp myapp

3.Define the Customer model:

In your app's directory (myapp), open models.py and add the following code:
from django.db import models

class Customer(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
phone_number = models.CharField(max_length=15)
address = models.CharField(max_length=200)
def __str__(self):
return self.name

18
4. After defining the model, run the following command to create the necessary
database tables:

>python manage.py makemigrations

>python manage.py migrate

5.In your app’s directory(myapp), openadmin.py and add the following code:
from django.contrib import admin
from .models import Customer

admin.site.register(Customer)

6.Create a view in views.py to retrieve customer details:

In your app's directory (myapp), open views.py and add the following code

from django.shortcuts import render


from .models import Customer
def customer_details(request):
customers = Customer.objects.all()
return render(request, 'customer_details.html', {'customers':
customers})

7.Create a template to display customer details:

In your app's directory (myapp), create a new directory called "templates" if it


doesn't exist.

Inside the templates directory, create a new HTML file called


"customer_details.html" with the following content
<!DOCTYPE html>
<html>
<head>
<title>Customer Details</title>
</head>
<body>

19
<h1>Customer Details</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Phone Number</th>
<th>Address</th>
</tr>
</thead>
<tbody>
{% for customer in customers %}
<tr>
<td>{{ customer.name }}</td>
<td>{{ customer.age }}</td>
<td>{{ customer.phone_number }}</td>
<td>{{ customer.address }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

8. Configure the URLs:

In your app's directory (myapp), open urls.py or create a new file called urls.py if it
doesn't exist.

Inside urls.py, add the following code:


from django.urls import path
from . import views
urlpatterns=[
path('customer-details/',views.customer_deatils,name='customer_details'),
]
9.Configure the project's main URL:

In your project's urls.py file, add the following code:


from django.contrib import admin
from django.urls import path,include

urlpatterns = [

20
path('admin/', admin.site.urls),
path('',include('myapp.urls')),
]

In settings.py add myapp


INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
]

We created a Django model named Customer with fields name, age,


phone_number, and address.

To add customer data, you can use the Django admin interface or create a form to
handle.

10.Create admin user

run the following command in cmd

>python manage.py createsuperuser

Then it will ask details like 'username', 'email', 'password', & 'confirm password'

11.Once the user is created run server. In command prompt run the following
command for running the server.

>python manage.py runserver

12.Open the browser and type the URL

13.http://127.0.0.1:8000/admin/

14.Login using above admin user credentials.

15.After logging in we are directed to site administration.

21
22
The customer details will be displayed on a web page when you visit
http://localhost:8000/customer-details/

23
2) Create a customer and order models and map them using one to
many relationships. Print all the orders made by customer in a web
page.

24
Here's an example of how you can create two Django models named Customer and
Order and establish a one-to-many relationship between them. You can then print
all the orders made by a customer on a web page.

1.Set up your Django project and app:

Create a new Django project: >django-admin startproject myproject

2.Create a new Django app within the project: >django-admin startapp myapp

3.Define the Customer and Order models:

In your app's directory (myapp), open models.py and add the following code:
from django.db import models

class Customer(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
phone_number = models.CharField(max_length=15)
address = models.CharField(max_length=200)
def __str__(self):
return self.name

class Order(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
order_number = models.CharField(max_length=20)
product = models.CharField(max_length=100)
quantity = models.IntegerField()
price = models.CharField(max_length=10)
def __str__(self):
return self.order_number

4.After defining the models, run the following command to create the necessary
database tables:

>python manage.py makemigrations

>python manage.py migrate

5.In your app’s directory(myapp), opne admin.py and add the following code:

25
from django.contrib import admin
from .models import Customer
from .models import Order

admin.site.register(Customer)
admin.site.register(Order)

6.Create a view in views.py to retrieve customer orders:

In your app's directory (myapp), open views.py and add the following code:

from django.shortcuts import render


from .models import Customer
def customer_orders(request, customer_id):
customer = Customer.objects.get(id=customer_id)
orders = customer.order_set.all()
return render(request, 'customer_orders.html', {'customer': customer,
'orders': orders})

7.Create a template to display customer orders:

In your app's directory (myapp), create a new directory called "templates" if it


doesn't exist.

Inside the templates directory, create a new HTML file called


"customer_orders.html" with the following content:

<!DOCTYPE html>
<html>
<head>
<title>Customer Orders</title>
</head>

26
<body>
<h1>Orders for {{ customer.name }}</h1>
<table>
<thead>
<tr>
<th>Order Number</th>
<th>Product</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
{% for order in orders %}
<tr>
<td>{{ order.order_number }}</td>
<td>{{ order.product }}</td>
<td>{{ order.quantity }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

8.Configure the URLs:

In your app's directory (myapp), open urls.py or create a new file called urls.py if it
doesn't exist.

Inside urls.py, add the following code:

from django.urls import path


from . import views
urlpatterns = [
path('customer-orders/<int:customer_id>/', views.customer_orders,
name='customer_orders'),
]

9.Configure the project's main URL:

In your project's urls.py file, add the following code:


from django.contrib import admin

27
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]

The order details of the customer will be displayed on a web page when you visit

http://localhost:8000/customer-orders/1/

http://localhost:8000/customer-orders/2/

28
29
30
31

You might also like