Chapter 15 : Creation of Django Based Web Application
Total Page:16
File Type:pdf, Size:1020Kb
CHAPTER 15 : CREATION OF DJANGO BASED WEB APPLICATION 1. What is Django? 2. What is the default URL of your Django project when you run it on builtin server? 3. What is the default port used by built in web server of Django? 4. What is the Django shortcut method to render an html response? 5. Differentiate between Django GET and POST method. 6. What are the features of Django? 7. What is the history of Django framework? 8. What is MVC? 9. What is MTV pattern of Django Architecture? 10. Write a program to perform read and write operation with .csv file. 11. Explain the importance of settings.py file and what data/ settings it contains. 12. Write the following commands for Django Project i) To create a project name XYZ company ii) to run the server of Django for XYZ companyen iii) to create an app name employee iv) write command to set the virtual environment 13. Mr Rajesh create a project name ABC in Django framework and an app in this project name friendchat but he forgot to register the app .Help Mr rajesh to register the app Friendchat with the project name ABC 14. Mr Karan created a project name “PQR” in Django. Name the file which is automatically created on creation of project PQR 15. Mrs Neelam create an app Friendbook inside the Django project folder name Messenger. Name the files which is created inside app friendbook automatically ANSWERS :CREATION OF DJANGO BASED WEB APPLICATION Ans1. Django s a free and open source web application framework, written in Python. A web framework is a set of components that helps you to develop websites faster and easier. Ans 2. http://127.0.0.1:8000 Ans 3. 8000 Ans 4. Render_to_response Ans 5 GET and POST. GET and POST are the only HTTP methods to use when dealing with forms. Django's login form is returned using the POST method, in which the browser bundles up the form data, encodes it for transmission, sends it to the server, and then receives back its response. Both of these are dictionary‐like objects that give you access to GET and POST data. POST data generally is submitted from an HTML <form> , while GET data can come from a <form>Top of Form Top of Formor the query string in the page's URL. Ans 6 1. Excellent Documentation 2. Python Web‐framework 3. SEO optimized 4. High scalability 5. versatile in nature 6. offers high security 7. Thorougly tested 8. provides rapid development Ans 7. Django was created by two web developers Adrian Holovaty and Simon Willison working at the Lawrence Journal‐World Newspaper in 2003. It was released publicly as a BSD license in July 2005. The framework was named after guitarist Django Reinhardt. The main reason behind Django’s existence is that Django inherited Python’s “batteries‐included” approach and includes pre‐made modules and applications for common tasks in web development like user authentication, templates, routes, and views, admin interface, robust security and support for multiple database backends. Ans 8. It is an acronym for Model View Controller. MVC pattern is a Product Development Architecture. It solves the traditional approach’s drawback of code in one file, i.e., that MVC architecture has different files for different aspects of our web application/ website. The MVC pattern has three components, namely Model, View, and Controller. 1. Model The Model is the part of the web‐app which acts as a mediator between the website interface and the database. In technical terms, it is the object which implements the logic for the application’s data domain. There are times when the application may only take data in a particular dataset, and directly send it to the view (UI component) without needing any database then the dataset is considered as a model. Although today if we want any kind of website we need to have some sort of database as we must be requiring some user input even if we are creating a simple blog site. 2. View This component contains the UI logic in the Django architecture. View is actually the User Interface of the web‐application and contains the parts like HTML, CSS and other frontend technologies. Generally, this UI creates from the Models component, i.e., the content comes from the Models component. 3. Controller The controller as the name suggests is the main control component. What that means is, the controller handles the user interaction and selects a view according to the model. The main task of the controller is to select a view component according to the user interaction and also applying the model component. This architecture has lots of advantages and that’s why Django is also based on this architecture. It takes the same model to an advanced level. Ans 9. Django is mainly an MTV (Model‐Template‐View) framework. It uses the terminology Templates for Views and Views for Controller. Template relates to the View in the MVC pattern as it refers to the presentation layer that manages the presentation logic in the framework and essentially controls the content to display and how to display it for the user. Thus our Python code will be in views and models and HTML code will be in templates. In MTV, the definitions of Model still remain the same that is, the Model contains the logical file structure of the project and is the middleware & data handler between database and view. The Model provides a definition of how the data formats as coming from the view so, it stores in the database and vice‐versa, i.e., the retrieving information from the database transfers to the view in the displayable format. The View in MTV architecture can look like the controller, but it’s not. The View in this MTV architecture is formatting the data via the model. In turn, it communicates to the database and that data which transfer to the template for viewing. The template is making the life of a frontend developer easy that’s for sure. It also provides more development speed then the traditional MVC architecture would. Template’s main goal is to keep everything that browser renders. The model’s data that’s coming from the server in different parts while integrating the same when the user interacts with the website. Here, the template layer in Django is more similar to the views layer in MVC pattern. This layer is more focused and with Django framework, it provides much more extendibility to the frontend developers than what MVC architecture was giving. 10 import csv def readcsv(): with open('C:\\Users\\ViNi\\Downloads\\data.csv','rt')as f: data = csv.reader(f) #reader function to generate a reader object for row in data: print(row) def writecsv( ): with open('C:\\Users\\ViNi\\Downloads\\data.csv', mode='a', newline='') as file: writer = csv.writer(file, delimiter=',', quotechar='"') #write new record in file writer.writerow(['4', 'Devansh', 'Arts', '404']) print("Press‐1 to Read Data and Press‐2 to Write data: ") a=int(input()) if a==1: readcsv() elif a==2: writecsv() else: print("Invalid value") 11 When Django server starts, it first looks for settings.py. As the name settings, it is the main settings file of your web application. Everything inside your Django project like databases, backend engines, middlewares, installed applications, main URL configurations, static file addresses, templating engines, allowed hosts and servers and security key stores in this file as a list or dictionary. So, when your Django server starts it executes settings.py file and then loads particular engines and databases so that when a request is given it can serve the same quickly. 12 i)django‐admin start project XYZ ii)python manage.py runserver iii)python manage.py startapp employee iv)virtualenv <projectname> 13 we add the name of app Friendchat with the INSTALLED_APPS sections of the file name settings.py 14 init_.py, settings.py,urls.py, wsgi.py 15 init_.py, admin.py, apps.py, models.py, test.py, views.py CHAPTER 16 :INTERFACE PYTHON WITH SQL Q1. What is MySQLdb? Q2. What is resultset? Q3. What is database cursor? Q4. What is database connectivity? Q5. Which function do use for executing a SQL query? Q6. Which package must be imported to create a database connectivity application? Q7. Differentiate between fetchone() and fetchall() Q8. How we can import MYSQL database in python? Q9. Write a query to rename the name of the artist from Towang to Tauwang. Q10. Write a query to delete an entity from the table Artists whose id is 1 Q11. Write a small python program to insert a record in the table books with attributes (title, isbn). Q. 12 Write a small python program to retrieve all record from the table books with attributes (title ,isbn). ANSWERS : INTERFACE PYTHON WITH SQL A1. MySQLdb is an open‐source freely available relational database management system that uses Structured Query Language. Now one of the most important question here is “What is SQL?” SQL (Structured Query Language) is a standard language for relational databases that allow users to do various operations on data like, Manipulating, Creating, Dropping, etc. In a nutshell, SQL allows you to do anything with the data. A2. Result set refers to a logical set of records that are fetched from the database by executing a query. A3. Database cursor is a special control structure that facilitates the row by row processing of records in the result set A4. Database connectivity refers to connection and communication between an application and a database system.