Django Record
Django Record
1
2) Create DJANGO project and app structure with django-admin commands
Output:
2
Steps for creating app inside project:
3
3) Deployment of project in server
Steps for deployment
4
<Press enter>
OUTPUT:
5
MODULE-2
1)Create template in Django Project to process user interface
Step 1: connect to drive >E:
6
Step 3: connect to project
(visual studio)
7
Step 6: create template inside employee
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>
<!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>
</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
<SAVE CHANGES>
13
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('', include('employees.urls')),
path('admin/', admin.site.urls),
]
<SAVE CHANGES>
Copy http://127.0.0.1:8000/
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 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:
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
STATIC_URL = 'static/'
MEDIA_URL='/images/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
16
</body>
</html>
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:
17
>django-admin startapp myapp
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:
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)
In your app's directory (myapp), open views.py and add the following code
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>
In your app's directory (myapp), open urls.py or create a new file called urls.py if it
doesn't exist.
urlpatterns = [
20
path('admin/', admin.site.urls),
path('',include('myapp.urls')),
]
To add customer data, you can use the Django admin interface or create a form to
handle.
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.
13.http://127.0.0.1:8000/admin/
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.
2.Create a new Django app within the project: >django-admin startapp myapp
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:
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)
In your app's directory (myapp), open views.py and add the following code:
<!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>
In your app's directory (myapp), open urls.py or create a new file called urls.py if it
doesn't exist.
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