Django Interview Questions

Q24. Mention the differences between Django, Pyramid and Flask.

Ans: 
  • Flask is a “microframework” primarily build for a small application with simpler requirements. In flask, you have to use external libraries. Flask is ready to use.
  • Pyramid is built for larger applications. It provides flexibility and lets the developer use the right tools for their project. The developer can choose the database, URL structure, templating style and more. Pyramid is heavy configurable.
  • Django can also used for larger applications just like Pyramid. It includes an ORM.

Q25. Discuss the Django architecture.

Ans: Django MVT Pattern:
Django Architecture - Python Interview Questions - EdurekaFigure:  Python Interview Questions – Django Architecture
The developer provides the Model, the view and the template then just maps it to a URL and Django does the magic to serve it to the user.

Q26. Explain how you can set up the Database in Django.

Ans: You can use the command edit mysite/setting.py , it is a normal python module with module level representing Django settings.
Django uses SQLite by default; it is easy for Django users as such it won’t require any other type of installation. In the case your database choice is different that you have to the following keys in the DATABASE ‘default’ item to match your database connection settings.
  • Engines: you can change database by using ‘django.db.backends.sqlite3’ , ‘django.db.backeneds.mysql’, ‘django.db.backends.postgresql_psycopg2’, ‘django.db.backends.oracle’ and so on
  • Name: The name of your database. In the case if you are using SQLite as your database, in that case database will be a file on your computer, Name should be a full absolute path, including file name of that file.
  • If you are not choosing SQLite as your database then settings like Password, Host, User, etc. must be added.
Django uses SQLite as default database, it stores data as a single file in the filesystem. If you do have a database server—PostgreSQL, MySQL, Oracle, MSSQL—and want to use it rather than SQLite, then use your database’s administration tools to create a new database for your Django project. Either way, with your (empty) database in place, all that remains is to tell Django how to use it. This is where your project’s settings.py file comes in.
We will add the following lines of code to the setting.py file:
1
2
3
4
5
6
DATABASES = {
     'default': {
          'ENGINE' : 'django.db.backends.sqlite3',
          'NAME' : os.path.join(BASE_DIR, 'db.sqlite3'),
     }
}

Q27. Give an example how you can write a VIEW in Django?

Ans: This is how we can use write a view in Django:
1
2
3
4
5
6
7
from django.http import HttpResponse
import datetime
def Current_datetime(request):
     now = datetime.datetime.now()
     html = "<html><body>It is now %s</body></html>" % now
     return HttpResponse(html)
Returns the current date and time, as an HTML document

Q28. Mention what the Django templates consists of.

Ans: The template is a simple text file.  It can create any text-based format like XML, CSV, HTML, etc.  A template contains variables that get replaced with values when the template is evaluated and tags (% tag %) that controls the logic of the template.
Django Template - Python Interview Questions - EdurekaFigure: Python Interview Questions – Django Template

Q29. Explain the use of session in Django framework?

Ans: Django provides session that lets you store and retrieve data on a per-site-visitor basis. Django abstracts the process of sending and receiving cookies, by placing a session ID cookie on the client side, and storing all the related data on the server side.
Django Framework - Python Interview Questions - EdurekaFigure: Python Interview Questions – Django Framework
So the data itself is not stored client side. This is nice from a security perspective.

Q30. List out the inheritance styles in Django.

Ans: In Django, there is three possible inheritance styles:
  1. Abstract Base Classes: This style is used when you only wants parent’s class to hold information that you don’t want to type out for each child model.
  2. Multi-table Inheritance: This style is used If you are sub-classing an existing model and need each model to have its own database table.
  3. Proxy models: You can use this model, If you only want to modify the Python level behavior of the model, without changing the model’s fields.

Copyright © 2013 SoftKul