Categories
Python Answers

How to do variable subtraction in Python Django templates?

Sometimes, we want to do variable subtraction in Python Django templates.

In this article, we’ll look at how to do variable subtraction in Python Django templates.

How to do variable subtraction in Python Django templates?

To do variable subtraction in Python Django templates, we can use the add filter.

For instance, we write

{{ myval|add:"-5" }}

to add -5 to myval by using the add filter with '-5' in the curly braces.

Conclusion

To do variable subtraction in Python Django templates, we can use the add filter.

Categories
Python Answers

How to get all the request headers in Python Django?

Sometimes, we want to get all the request headers in Python Django.

In this article, we’ll look at how to get all the request headers in Python Django.

How to get all the request headers in Python Django?

To get all the request headers in Python Django, we can use the request.headers property.

For instance, in our view, we write

request.headers['User-Agent']

to get the User-Agent request header.

request.headers is a dict.

Conclusion

To get all the request headers in Python Django, we can use the request.headers property.

Categories
Python Answers

How to pass query parameters via Python Django’s {% url %} template tag?

Sometimes, we want to pass query parameters via Python Django’s {% url %} template tag.

In this article, we’ll look at how to pass query parameters via Python Django’s {% url %} template tag.

How to pass query parameters via Python Django’s {% url %} template tag?

To pass query parameters via Python Django’s {% url %} template tag, we just append it after the URL.

For instance, in our template, we write

<a href="{% url 'myview' %}?office=foobar">
  ...
</a>

to add ?office=foobar after the URL for the myview view.

Conclusion

To pass query parameters via Python Django’s {% url %} template tag, we just append it after the URL.

Categories
Python Answers

How to only accept a certain file type in FileField with Python Django?

To only accept a certain file type in FileField with Python Django, we can add a validator function to check the file extension.

For instance, we write

def validate_file_extension(value):
    import os
    from django.core.exceptions import ValidationError
    ext = os.path.splitext(value.name)[1]  # [0] returns path+filename
    valid_extensions = ['.pdf', '.doc', '.docx', '.jpg', '.png', '.xlsx', '.xls']
    if not ext.lower() in valid_extensions:
        raise ValidationError('Unsupported file extension.')

class Document(models.Model):
    file = models.FileField(upload_to="documents/%Y/%m/%d", validators=[validate_file_extension])

to define the validate_file_extension function to check the file extension of the selected file’s path.

We get the extension from the path with os.path.splitext(value.name)[1].

And then we check if the ext extension is in valid_extensions.

If it’s not, then we raise a ValidationError.

Next, in our Document model, we add a FileField with the validators arguments set to an array with the validate_file_extension as the value.

Then validate_file_extension when a file is uploaded.

Categories
Python Answers

How to use a UUID as a primary key in Python Django models?

To use a UUID as a primary key in Python Django models, we can create a UUIDField.

For instance, we write

import uuid
from django.db import models

class MyUUIDModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

to create the id UUIDField in MyUUIDModel.

And then we set the default value to the value returned by uuid.uuid4.

We set primary_key to True to make id the primary key column.