Top Django Interview Questions

 

This time, i had compiled some Django Interview Questions. 

This List might come handy to both Interviewer and Interviewee. 

  1. How do you create new project in Django ?
  2. How do you create new app in Django ?
  3. Explain MVT architecture of Django ?
  4. What are Middlewares ?
  5. How do you create custom Middleware ? What special methods are used to handle request/response/exception ?
  6. In what order does middlewares listed in settings.py are executed when request comes ? Top to bottom or bottom to up.
  7. In what order does middlewares listed in settings.py are executed when response goes ? Top to bottom or bottom to up.
  8. Explain Django ORM(Object-relational mapper) ?
  9. How does session framework works in Django ?
  10. What is CSRF ? How can you prevent csrf attack ?
  11. What are Django forms ?
  12. What are Django Models ?
  13. How do you migrate your tables in Django ?
  14. What are model manager and how do you write custom manager ?
  15. What are Django Templates ? How template inheritance works in Django ?
  16. What is the use of wsgi.py file ?
  17. What are signals ? How do you create custom signals in Django ?
  18. What are receiver functions in Django ?
  19. What are Django template tags ?
  20. How do you create custom template tags ?
  21. What are generic views ? How many types of generic views are there ?
  22. What’s the difference between select_related and prefetch_related in Django ORM?
  23. What is the use of context processors in Django ?
  24. What 3rd party Django apps you have used ?
  25. What are Python Virtual Environments ?

 

Advertisements

Deploy Django Project Using NGINX and Gunicorn

Meet Django

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

In this guide, we will try how to install and configure some components on Ubuntu 14.04 to support and serve Django applications.

Meet NGINX

NGINX accelerates content and application delivery, improves security, facilitates availability and scalability for the busiest web sites on the Internet.

Meet Gunicorn

Gunicorn ‘Green Unicorn’ is a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model ported from Ruby’s Unicorn project. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy. This Gunicorn will serve as an interface to our application, translating client requests in HTTP to Python calls that our application can process.

Things we will try to achieve :

  1. Installing Django within a virtual environment.
  2. Start Django project from scratch.
  3. Host this Django project using Nginx and Gunicorn.

Installing Django within a virtual environment:

In-order to have django within virtual environment we need to execute virtualenv command. We can install this with pip.

For Python 2:

sudo pip install virtualenv

Python 3:

sudo pip3 install virtualenv

As now we have our virtualenv ready, we can go ahead and setup projects directory. 

  • mkdir ~/myproject
  • cd ~/myproject

Within this project directory, create a Python virtual environment by typing:

virtualenv myproject_env

By executing this command “myproject_env” named directory will be created. It will be used to install and configure different python environments.

Now to activate this virtual environment, execute following command.

source myproject_env/bin/activate

With your virtual environment activated, We can move forward by installing Django, Gunicorn.

pip install django gunicorn

Components which are installed under virtual environment are isolated from other os global environments.

Start Django project from scratch.

Now with virtual environment been activated, Start django project using django management command.

django-admin.py startproject myproject .

Now open settings.py fiile located under myproject folder.

Edit this settings.py by replacing following code :

ALLOWED_HOSTS = ['your_server_domain_or_IP',]

STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

Complete remaining basic project setup

cd ~/myproject

./manage.py makemigrations

./manage.py migrate

./manage.py createsuperuser

./manage.py runserver 0.0.0.0:8000
In web browser, enter your_ip_address:8000:

Once you had finished exploring recently created project, hit CNRL + C.

Now let’s test Gunicorn interface.

cd ~/myproject

gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application

This will start gunicorn on same interface on which our earlier development server was working.

Now type this command to deactivate the virtual environment:

deactivate

Now we have to create Gunicorn systemd service file.

This service file will be used to start or stop our application server.

Open the gunicorn systemd service file using this command.

sudo vi /etc/systemd/system/gunicorn.service

Edit this service file with following data.

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=vivek
Group=www-data
WorkingDirectory=/home/vivek/myproject
ExecStart=/home/vivek/myproject/myproject_env/bin/gunicorn --workers 3 --bind unix:/home/vivek/myproject/myproject.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target

Save and close the file.

Start the Gunicorn service using following command.

sudo systemctl start gunicorn

sudo systemctl enable gunicorn

This will create socket file inside your project directory.

Always check that the socket file has www-data group and this group should have group ownership.

Now is the time to configure NGINX

Create new server block in Nginx’s sites-available directory.

sudo vi /etc/nginx/sites-available/myproject

Now edit this server block with following code.

server {
    listen 80;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/vivek/myproject;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/vivek/myproject/myproject.sock;
    }
}

Save and close the file.

Now go forward by enabling the file by linking it to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

Test your Nginx configuration for syntax errors by typing:

sudo nginx -t

If no errors is found, Restart Nginx by typing:

sudo systemctl restart nginx

In the end, Following points were achieved :

  1. Virtual environment having its own Django project.

  2. Django can now handle client requests thanks to Gunicorn.

  3. Nginx is added to act as a reverse proxy to handle client connections and serve the project.

How to create Auto Destroying Messages in Django ?

Auto Destroying Message

  • Auto Destroy Message after Reading
  • Auto Destroy functionality comes handy when you want to share critical data with someone like user credentials.

AIM

  • Create Mission Impossible like auto deletion message.
  • Maintain high level of encryption.
  • No loop holes so handle DB attack also.

Features

  • Creates Encrypted Messages.
  • Unique hashed URL is provided on message creation this can be communicated to other user.
  • The message self destroy itself after reading from the user side.
  • Even if DB security is compromised, no-one will be able to access the message which is send to other user as everything is encrypted.
  • When the message is read, we deletes the message and save the IP address of the user and the time of reading the note.
  • Bootstrap UI is provided.

GITHUB