StackTips

Most Popular Django Interview Questions and Answers

stacktips avtar

Written by

Editorial,  5 min read,  2.98K views, updated on Sept. 29, 2024

1. How to check if the user instance is admin Django permissions?

The is_superuser property in the User model designates that this user has all permissions without explicitly assigning them.

class UserAdmin(BaseUserAdmin):
    ...
    def has_add_permission(self, request, obj=None):
        return request.user.is_superuser

    def has_delete_permission(self, request, obj=None):
        return request.user.is_superuser

2. How to get the Django admin URL for a model object?

You can use the URL resolver directly in a template, there's no need to write your filter. E.g.

{% url 'admin:index' %}
{% url 'admin:polls_choice_add' %}
{% url 'admin:polls_choice_change' choice.id %}
{% url 'admin:polls_choice_changelist' %}

ref: Django official documentation

3. What is a "slug" in Django?

A "slug" is a way of generating a valid URL, generally using data already obtained. For instance, a slug uses the title of an article to generate a URL. I advise to generate the slug by means of a function, given the title (or another piece of data), rather than setting it manually. An example:

<title> The 46 Year Old Virgin </title>  
<content> A silly comedy movie </content>  
<slug> the-46-year-old-virgin </slug>

Now let's pretend that we have a Django model such as:

class Article(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField(max_length=1000)
    slug = models.SlugField(max_length=40)

How would you reference this object with a URL and with a meaningful name? You could for instance use Article. id so the URL would look like this:

www.example.com/article/23

Or, you might want to reference the title like this:

www.example.com/article/The 46 Year Old Virgin

Since spaces aren't valid in URLs, they must be replaced by %20, which results in:

www.example.com/article/The%2046%20Year%20Old%20Virgin

Both attempts are not resulting in very meaningful, easy-to-read URL. This is better:

www.example.com/article/the-46-year-old-virgin

In this example, the-46-year-old-virgin is a slug: it is created from the title by down-casing all letters, and replacing spaces with hyphens -

4. What is a Django middleware, and how is it used?

A Django middleware is a component that sits between the web server and the view function and can perform operations on incoming requests or outgoing responses. Middleware can be used to perform tasks such as authentication, caching, logging, or modifying headers.

Keep exploring

Let’s be friends!

🙌 Stay connected with us on social media for the latest updates, exclusive content, and more. Follow us now and be part of the conversation!