[Type C] Chapter 15 Sumita Arora Solutions | Class 12 Computer Science

Here is class 12 computer science [Type C] Unit 15 solutions for Sumita Arora back exercise assignment. Below includes both textual and video solutions wherever required. View all the answers in assignment for chapter 15 and for all chapters here.

Sumita Arora Solutions for all lessons here.

Q.1: Activate virtual environment and then do the following:
(a) Write commands to create a Django project namely MyMart having two apps in it namely list and order.

(b) Register these apps with the project.

(c) Create a templates folder in the base directory of your project.

(d) Register the templates folder created with the project.

(e) The list app intends to show list of items available.  For this, it has an html page that displays a list of items as shown below:
Create an html file namely list.html for this and store it under templates folder of your project.

(f) The order app intends to display a form shown below:
Create an html file namely order.html that displays this form and store it in the templates folder.

(g) Create a view function namely mylist for list app that renders list.html (in ..list\views.py).

(h) Create a view function namely myorder for order app that renders  order.html  in ..order\views.py). 
Add POST request processing to it so that it stores the received form data in a CSV file.

(i) in the urls.py file of your project’s web-application folder (inner project name folder), import the two apps’ views.py modules by adding following import commands:
from list import views as listviews
from order import views as ordviews
Recall that the default address of your local web server is localhost\8000\. We are referring to this address as <server> in question below:

(j) Create a URL Conf for <server>/list/ URL that links to mylist view function created above.  Use the name listviews.mylist for mylist function (as you imported list\views.py as listviews)

(k) Create a URL Conf for <server>/order/ URL that links to myorder view function created above.  Use the name ordviews.myorder for myorder function (as you imported order\views.py as orderviews).

(l) Run server for your project.  And also open a web browser window.

(m) Type url as localhost:8000/list/ in the web browser’s address bar.

(n) Type url as localhost:8000/order/ in the web browser’s address bar.

(o) Check if your entered data has created any csv file in the base directory folder.  If yes, show the contents of the csv file.

(a) Write commands to create a Django project namely MyMart having two apps in it namely list and order. 
Answer:

command to create project: 
django-admin startproject MyMart
command to create app:
python manage.py startapp list
python manage.py startapp order

(b) Register these apps with the project.
Answer:
go to  settings.py in MyMart folder and add names of apps in INSTALLED_APPS list

(c) Create a templates folder in the base directory of your project.
Answer:
create templates folder in base directory MyMart


(d) Register the templates folder created with the project.
Answer:
go to MyMart settings.py add os.path.join(BASE_DIR, ‘templates’) in DIRS in templates


(e) The list app intends to show list of items available.  For this, it has an html page that displays a list of items as shown below:

Create an html file namely list.html for this and store it under templates folder of your project.
Answer:

Code:
<!DOCTYPE html>
<html>
    <title>list</title>
    <style>
        body{
            text-align: center;
            margin-left: auto;
            margin-right: auto;
            display: table;
            padding: 50px;
        }
        h2{
            font-family:"Arial Black", Gadget, sans-serif; 
            color : #a8329e;
        }
        .multi-column {
            -moz-column-count: 2;
            -moz-column-gap: 20px;
            -webkit-column-count: 2;
            -webkit-column-gap: 20px;
            column-count: 2;
            column-gap: 20px;
            text-align: left;
            padding: 20px;            
        }
        li{
            padding: 10px;
        }
    </style>
<head>
<body>
    <h2>ITEMS LIST</h2>
    <div>
        <ol class="multi-column" >
            <li>Bath tubs</li>
            <li>Battery Charger</li>
            <li>Battery Eliminator</li>
            <li>Beam Scale (upto 1.5 tons)</li>
            <li>Belt leather strap</li>
            <li>Brass Wire</li>
            <li>Bituminous Paints</li>
            <li>Blotting Paper</li>
            <li>Bolts & Nuts</li>
            <li>Bolts Sliding</li>
            <li>Bone Meal</li> 
            <li>Boot Polish</li>
        </ol> 
    </div> 
</body>
</head>
</html>

(f) The order app intends to display a form shown below:
Create an html file namely order.html that displays this form and store it in the templates folder.

Answer:

Code:
<!DOCTYPE html>
<html>
<head>
<title>order</title>
<style>
    body{
        margin-left: auto;
        margin-right: auto;
        display: table;
        padding: 50px;
    }
    input{
        margin: 5px;
    }

</style>
</head>
<body>
    
    {{ message }}
    <form action="" method="POST">
        {% csrf_token %}
        <label class="customer" >Customer Name:</label>
        <input type="text"name="customerName">
        <br>
        <label >Customer Address:</label>
        <input type="text" size="50" name="customerAddress">
        <br>
        <label>Order Item Number:</label>
        <input type="text" name="itemNumber">
        <br>
        <label>Order Item Qty:</label>
        <input type="text" name="itemQty">
        <br>
        <input type="submit" value = "place order" >
    </form>
</body>
</html>

(g) Create a view function namely mylist for list app that renders list.html (in ..list\views.py).
Answer:

def mylist(request):
   return render(request,'list.html')



(h) Create a view function namely myorder for order app that renders order.html in ..order\views.py). 
Add POST request processing to it so that it stores the received form data in a CSV file.

Answer:

Code:
def myorder(request):
    if request.method == 'POST':
        print("inside post")
        name = request.POST['customerName']
        address = request.POST['customerAddress']
        itemNumber = int(request.POST['itemNumber'])
        itemQty = int(request.POST['itemQty'])
        row = [name, address, itemNumber, itemQty]
        filename = "order_records.csv"
        with open(filename, 'w') as csvfile:  
            csvwriter = csv.writer(csvfile)  
            csvwriter.writerow(row) 
        messages.info(request, 'Wrong Username or Password...')
        return render(request,'order.html')
   return render(request,'order.html')



(i) in the urls.py file of your project’s web-application folder (inner project name folder), import the two apps’ views.py modules by adding following import commands:
from list import views.py as listviews

from order import views.py as ordviews
Recall that the default address of your local web server is localhost\8000\. We are referring to this address as <server> in question below:
Answer:


(j) Create a URL Conf for <server>/list/ URL that links to mylist view function created above.  Use the name listview.mylist for mylist function (as you imported list/views.py as listviews)
Answer:

urlpatterns = [path('admin/', admin.site.urls),
    path('list/',listviews.mylist, name='list'), #for list
]

(k) Create a URL Conf for <server>/list/ URL that links to mylist view function created above.  Use the name listview.mylist for myorder function (as you imported order/views.py as orderviews).
Answer:

urlpatterns = [    path('admin/', admin.site.urls),
    path('list/',listviews.mylist, name='list'), #for list
    path('order/',ordviews.myorder, name='order')#for order
]



(l) Run server for your project.  And also open a web browser window.
Answer:
python manage.py runserver


(m) Type url as localhost:8000/list/ in the web browser’s address bar.
Answer:

http://127.0.0.1:8000/list/



(n) Type url as localhost:8000/order/ in the web browser’s address bar.
Answer: http://127.0.0.1:8000/list/

(o) Check if your entered data has created any csv file in the base directory folder.  If yes, show the contents of the csv file.
Answer:

File content:
bachan,21,101,5

rajni,5,1208,45

mohanlal,chennai,3,12

pritesh,pune,134,2

sonakshi,indore,34,1

ankit,delhi,1252,12

Clear Doubts with Computer Tutor
In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in

You cannot copy content of this page