Skip to main content

Python's Web Framework - Django Fundamentals

image source: djangoproject


What is  middleware in Django?
A middleware is a class with methods that are globally executed during the request or response phase. For example, Django includes a middleware component called AuthenticationMiddleware which associates users with requests using sessions. See Doc

What are the models added by the Django authentication framework?
Django authentication framework includes the following models:
User: A user model with basic fields; the main fields of this model are username, password, email, first_name, last_name, and is_active.
Group
: A group model to categorize users.Permission: Flags for users or groups to perform certain actions.

Which parameters is accepted by the render method? (django.shortcuts -> render)?
Render accepts three parameter. (request, template_name, context)
For example: return render(request, 'account/login.html', {'form': form})

What is SMTP  and why do we use it? 
SMTP (Simple Mail Transfer Protocol) is a communication protocol for electronic mail transmission. Let's say the user has forgotten his password. Now the system should send an email with a link to reset his new password. In that case, we have to use SMTP. In case of Django, we to configure it in the settings.py file. However,, you can configure Django to write emails to the
standard output instead of sending them through an SMTP server.
Django provides an email backend to write emails to the console.


What does the on_delete do on Django models? 
This is not specific to Django, it's a SQL standard. This means if you delete some object, will it delete the other objects related to it or not.
There are six possible actions can be taken on the on_delete parameter.
The most common two are:
CASCADE: When the referenced object is deleted, it also deletes the other objects associated with it. For example, If you want to delete a blog post, it should also delete the comments associated with that post.
PROTECT: It avoids the deletion of the referenced objects.

See other four types of operations here in Stack Overflow.

What is the difference between null = True and blank = True in Django? 
null=True sets NULL on the column in your DB. Blank values for Django field types such as DateTimeField or ForeignKey will be stored as NULL in the DB.

blank=True determines whether the field will be required in forms. This includes the admin and your own custom forms. If blank=True then the field will not be required, whereas if it's False the field cannot be blank.

The combo of the two is so frequent because typically if you're going to allow a field to be blank in your form, you're going to also need your database to allow NULL values for that field. The exception is CharFields and TextFields, which in Django are never saved as NULL. Blank values are stored in the DB as an empty string (''). [Ref]
It is wrong to define null = True, for String-based fields like CharField and TextField.

You can follow this table to avoid confusion.
Example is given below:

How to do 'not eqaul' in Django queryset filtering? 
You can do the following:
>>> Entry.objects.filter(~Q(id=1))

What is slug in Django?
It is a way of generating valid URL, normally using data already exists. For example, generating a URL using the title of a blog post.

How to check Django version?
Open a python console and type:
>>>import django
>>>django.VERSION

How to extend the User model with custom fields in Django?
Django-recommended way to this is using the
OneToOneField(User) property. 

How to reset Django admin password?
python manage.py changepassword <user_name>

How to revert last migration in Django?
For Django 1.8+, you can see the names of all migrations using:
python manage.py showmigrations app_name
To reverse all migrate:
python manage.py app_name zero

What is the difference between django OneToOneField and ForeihnKey?
Conceptually, a one-to-one relationship is the same as ForeignKey with unique=True. 

A ForeignKey is for one-to-many relation. For example, a car may have many wheels, each wheel having a ForeignKey to the car it belongs. A one-to-one relation would be for an engine, where a Car object has only one engine.. [Ref

What is the difference among one-to-one, many-to-one, and many-to-many relationship in Django? 
Best answer for this question can be found here in Stack Overflow

What is PEP8?
PEP8 is a style guide for writing Python code. It is a set of rules for formatting Python code to maximize its readability. Rules can be found here.

How to do aggregate queries in Django? 
A cheat sheet is given for this kind of queries in djangoproject.

If you want to upload files using HTML forms, then would be the structure of form?
Though it not specific to Django, all other language should also use the following structure: 
the method should be post. enctype would be multipart/form-data. For example: 

<form action="." method="post" enctype="multipart/form-data">
.........
</form>

How to use custom user model?
Django offers a way to substitute the whole user with your own custom model. Your user class should inherit from Django's AbstractUser class. You can read more about this method from here.

What are the methods offered by Django messages framework?
The messages framework provides a simple way to add messages to users. Messages are stored in a cookie by default. We can create a new message using the add_message() method or any of the following shortcut methods:
success()
info()
warning()
error()
debug()
Learn more about message framework here.

How to add social authentication to your site using Django?
If you want to add social authentication to your site using services like Facebook, Twitter, Google, Python social auth is a Python module that simplifies the process. To add this to your Django app, you need to install social-auth-app-django module.
You can do so by using
pip install social-auth-app-django
Then, add social_django to your INSTALLED_APPS setting in the setting.py file of your project.
 A good article for adding social auth in your application can be found here .






Comments

  1. I must say, this is really an awesome post which you have shared in a simple and easy to grasp way. Thanks for sharing this post.Full Stack Development Company Texas

    ReplyDelete
  2. Your writing style is too good, its is very very helpful for all of us
    sales enablement app

    ReplyDelete

Post a Comment

Popular posts from this blog

Difference between abstract class and interface in OOP

Source: Amit Sethi In Interface: > All variables must be public static final. > No constructors. An interface can not be instantiated using the new operator.   > All methods must be public abstract .  

DFS Performance Measurement

Completeness DFS is not complete, to convince yourself consider that our search start expanding the left subtree of the root for so long path (maybe infinite) when different choice near the root could lead to a solution, now suppose that the left subtree of the root has no solution, and it is unbounded, then the search will continue going deep infinitely, in this case , we say that DFS is not complete. Optimality  Consider the scenario that there is more than one goal node, and our search decided to first expand the left subtree of the root where there is a solution at a very deep level of this left subtree , in the same time the right subtree of the root has a solution near the root, here comes the non-optimality of DFS that it is not guaranteed that the first goal to find is the optimal one, so we conclude that DFS is not optimal. Time Complexity Consider a state space that is identical to that of BFS, with branching factor b, and we start the search from th

Difference between a Singly LinkedList and Doubly LinkedList