60+ Python django interview questions and answers

Spread the love

If you are looking for django practical interview questions for freshers and experienced professions. Here is the list of 55+ Latest Python django interview questions.

  1. What is Django ?

Django is a high-level Python-based free and open-source web framework, which follows the model-view-template architectural pattern.

  1. What is latest and stable version of Django ?

The latest and stable version  is version 2.2.1.

  1. Why django ? What is advantages of Django ?

Its free and open source, it makes the web development process very easy and the developer can fully focus on the designing process and boost performance. It is an ideal tool for startups, where web design is the need to bring out the real concept and prospects of the company.

It makes it easier to build web apps more quickly and with less code, It is also ridiculously fast, reassuringly secure and exceedingly scalable.

  1. What are features of Django ?

Django’s features are: Rapid Development, Secure, Scalable, Fully Loaded, Versatile, Open Source, Vast and Supported Community.

  1. What id CRUD ?

CRUD means Create, Read, Update and Delete. It provides a memorable framework for reminding developers of how to construct full, usable models when building application programming interfaces (APIs).

  1. What are disadvantages of Django ?

Django’s disadvantages are that the URL specifying with regular expressions is not an easy task to accomplish, at least for beginners, It’s a strongly opinionated framework, which gives it a monolithic feeling. Also, template errors fail silently by default,.

  1. Explain the architecture of Django?

Django Architecture is based on Model View Controller (MVC) pattern. It consists of 3 different parts:

  • The Model which is the logical data structure behind the complete application & represents by a Database.
  • The View which is the user interface that you see in the browser whenever you visit a website.
  • The Controller which is the middleman that connects the view & model together. I
  1. Can you explain the working philosophy of Django?

Django’s working philosophy can be broken into many components:

Models.py file: This file defines your data model by extending your single line of code into full database tables and add a pre-built administration section to manage content.

Urls.py file: It uses a regular expression to capture URL patterns for processing.

Views.py file: It is the main part of Django. The actual processing happens in view.

When a visitor lands on Django page:

  1. Django checks the various URLs pattern you have created and uses the information to retrieve the view.
  2. The view processes the request, querying your database if necessary.
  3. The view passes the requested information on to your template.
  4. The template renders the data in a layout you have created and displays the page.

 

  1. What is MVT and MVC, How it’s related to Django?

A common way of explaining Django’s architecture in terms of MVC is to describe it as a Model-View-Template (MVT). There’s no difference between MTV and MVT, by the way – they’re two different ways of writing exactly the same thing, which just adds to the confusion.

  1. How to install django ?

To install Django, we need to download and install Python as required depending on the operating system in use. t’s recommended  to create a virtual environment. Run the command pip install “django>=2.2,<3” on the terminal and wait until it’s successfully installed.

  1. How to check the latest version of Django?

To check the latest version of Django, visit their homepage https://www.djangoproject.com/ and on the right of the page you’ll see a “Download latest release” button with the latest version on it.

  1. How can you setup Database in Django?

Create the initial Django project skeleton by using the django-admin startproject command.

Open the settings from the settings.py file to confirm all settings such as your timezone are suitable for your project, else make changes or add new settings where necessary.

Install MySQL Database Connector.

Create the database.

Add the MySQL Database Connection to your Application.

Test MySQL Connection to Application.

  1. What is the list of backends supported by django ?

Backends include: IBM DB2, Microsoft SQL Server, Firebird and ODBC.

  1. Can i Connect mysql to django ?
    Yes
  2. Can i Connect plsql to django ?
    Yes
  3. How to connect mongodb in Django ?

MongoDB can be connected to Django by adding the DATABASES object with its default object in the settings.py file and specifying the ENGINE’s value as django and the NAME’s value as the name of the MongoDB database you want to connect to.

 

  1. How can you set up static files in Django?

Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.

In your settings file, define the STATIC_URL to the folder path string where your static files will be placed and make sure the folder exists.

In your templates, use the static template tag to build the URL, for the given relative path using the configured STATICFILES_STORAGE.

Store your static files in the static folder you specified. In addition to using a static directory inside your app, you can define a list of directories (STATICFILE_DIRS) in your settings file where Django will also look for static files.

  1. What is the use of session framework in Django?

Session framework lets you store and retrieve arbitrary data on a per-site-visitor basis. It stores data on the server-side and abstracts the sending and receiving of cookies each containing a session ID unless you are using a cookie-based backend.

  1. What is the usage of middlewares in Django?

Middlewares are used  go to modify the request i.e. HttpRequest object which is sent to the view, to modify the HttpResponse object returned from the view and to perform an operation before the view executes.

  1. What are the roles of receiver and sender in signals?
  • The receiver is the callback function which will be connected to a signal
  • The sender specifies a particular sender to receive signals from.
  1. What does Django templates contain ?

Django templates contains the static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.

  1. How to create super user in django ?

To create a super user,,

  • Create project using the django-admin startproject command.
  • Move into the project location and run python manage.py makemigrations && python manage.py migrate && python manage.py createsuperuser
  1. How to create simple application in django ?

To create a simple application  use the command django-admin startproject followed by the application’s name.

  1. What is ORM ? Advantages of ORM ?

ORM (Object-relational mapping) is a programming technique for converting data between incompatible type systems using object-oriented programming languages.

Advantages include:

  • Concurrency support
  • Cache management
  1. How to create a model in django ?

Add the model object in the models.py file, updated settings for the newly created app by adding it to the INSTALLED_APPS section in settings.py, make migrations, and verify the database schema.

  1. What is migration in django ?

Migrations are a way of propagating changes made in the model into the database schema (adding a field, deleting a model, etc.)

  1. How to do migrations in django ?

To do migrations , create or update a model and in the app directory, run the command ./manage.py makemigrations <app name> && ./manage.py migrate <app name>

  1. How to clear cache in django ?

To clear cache, run the clear() method from django.core.cache in a python script.

  1. What is Rest API ?

A REST API is an application program interface that uses HTTP requests to GET, PUT, POST and DELETE data.

  1. How to Create APIs in Django ?

Create a project directory,  create python virtual environment,   and activate it, install Django and djangorestframework using the pip install command. In the same project directory, create  project using the command django-admin.py startproject api. Start the app. Add the rest_framework and the Djano app to INSTALLED_APPS to settings. Open the api/urls.py and add urls for the Django app. We can then create models and make migrations, create serializers, and finally wiring up the views.

  1. What is DRF of Django Rest Frame work ?

Django Rest Framework (DRF) is a powerful module for building web APIs. It’s very easy to build model-backed APIs that have authentication policies and are browsable.

  1. How to Fetch data from apis using Django ?

We use the Fetch API and SessionAuthentication  by adding it to the settings.py file on the server and on the client, include  the getCookie method. Finally, use the fetch method to call your endpoint.

  1. How to update the data from apis ?

We update data by sending PUT requests. Add a new path in the data model urlpatterns from which the update will be sent to. We then add an update method to the serializer that will do the update.

  1. What is Authentication ?

Authentication is the process or action of verifying the identity of a user or process.

  1. Types of Authentication in REST API ?

Token based authentication and Session based authentication.

  1. What is token based authentication system ?

A token based authentication system is a security system that authenticates the users who attempt to log in to a server, a network, or some other secure system, using a security token provided by the server

  1. Can i use django apis in mobile application development ?  Yes
  1. Explain Mixins in Django ?

A mixin is a special kind of multiple inheritance. There are two main situations where mixins are used: to provide a lot of optional features for a class and to use one particular feature in a lot of different classes

  1. Different types caching strategies in django ?

Different types of  caching strategies  include Filesystem caching, in-memory caching, using memcached and database caching.

  1. How a request is process in Django ?

When the user makes a request of your application, a WSGI handler is instantiated, which:

  • imports your settings.py file and Django’s exception classes.
  • loads all the middleware classes it finds in the MIDDLEWARE_CLASSES or MIDDLEWARES(depending on Django version) tuple located in settings.py
  • builds four lists of methods which handle processing of request, view, response, and exception.
  • loops through the request methods, running them in order
  • resolves the requested URL
  • loops through each of the view processing methods
  • calls the view function (usually rendering a template)
  • processes any exception methods
  • loops through each of the response methods, (from the inside out, reverse order from request middlewares)
  • finally builds a return value and calls the callback function to the web server
  1. When to use iterator in Django ORM ?

The iterator is used when  processing results that take up a large amount of available memory (lots of small objects or fewer large objects).

  1. What are signals in Django ?
    Signals allow certain senders to notify a set of receivers that some action has taken place. They’re especially useful when many pieces of code may be interested in the same events.
  2. How to implement social login authentication in Django ?Run the development server to make sure all is in order. The install python-social-auth using the pip install command. Update settings.py to include/register the library in the project  Update the database by  making migrations. Update the Project’s urlpatterns in urls.py to include the main auth URLs. Create a new app https://apps.twitter.com/app/new and make sure to use the callback url http://127.0.0.1:8000/complete/twitter. In the project directory, add a config.py file and grab the consumer key and consumer secret and add them to the config file. Finally add urls to the config file to specify the login and redirect urls. Do a sanity check and add friendly views.
  1. Where to store static files in django ?

Static files are stored in the folder called static in the Django app.

  1. Types of Session handling methods in Django

Session handling types  include database-backed sessions, Using cached sessions, Using file-based sessions, and Using cookie-based sessions.

  1. What does Of Django Field Class types do?
  • The database column type
  • The widget to use ’s forms and admin site,.
  • he minimal validation requirements, which are used ’s admin interface and by forms.
  1. How to use file based sessions?

To use file-based sessions, set the SESSION_ENGINE setting to “django.contrib.sessions.backends.file” or the SESSION_FILE_PATH setting (which defaults to output from tempfile.gettempdir(), most likely /tmp) to control where Django stores session files. Be sure to check that your Web server has permissions to read and write to this location.

  1. Is Django Open source ?

Yes

  1. How to set and unset session in django ?

To set a session , set a key and a value in the session object. To unset a session Django, use the in-built Python del keyword to delete the session by specifying the key.

  1. How to create views in django ?

To create views , create and activate a virtual environment.  Create a file called views.py for example and create view functions to render views using the HttpResponse object in this file and  map URLs to views by adding the path to urlpatterns in the urls.py file.

  1. What is url mapping and how to do it in django ?

To map an URL,  open the urls.py file and add a path entry to the urlpatterns array specifying the path string, the view function, and the name of the view function.

  1. What are population websites built on django ?

Sites include Disqus, Instagram, Knight Foundation, MacArthur Foundation.

  1. How to create custom sql query in django ?

To create custom sql query ,use the database connection, call connection.cursor() to get a cursor object. Then, call cursor.execute(sql, [params]) to execute the SQL.

  1. Types of Model relationships in django ?

Types of model relationships  include: One to many, Many to many,  One to one model relationships.

  1. Types of inheritances in django ?

Types of inheritances  include: Single Table Inheritance , Multi Table Inheritance and ContentTypes

  1. What is context in django ?

Context is a dictionary with variable names as the key and their values as the value. A Context object is the context in which the template is being rendered.

  1. How to fetch data through objects in django ?

Use  the model to access the database and Django will execute the SQL.

  1. What is QuerySet in django ?

queryset is a collection of (sql) queries, in your example above print(b.query) will show you the sql query generated from your django filter calls.

  1. How to make Foreign Key relation in django ?

To make a foreign key relation , create a many-to-one relationship by calling the ForeignKey() function on the model with a value of the key’s name.

  1. How to setup django and apache in server ?

It is done by using mod_wsgi. Make sure you have Apache and mod_wsgi installed.  Let’s say we want to share our project (myproject) with Apache.Set Apache to access our folder. Assume we put our myproject folder in the default “/var/www/html”. At this stage, accessing the project will be done via 127.0.0.1/myproject. Configure Apache in httpd.conf. By adding a <Files wsgi.py>  file configuration in it.

 

Author: admin

Leave a Reply

Your email address will not be published. Required fields are marked *