Why do you need to use this "f" with dynamic http responses in django?

Viewed 24

For example:

from django.shortcuts import render
from django.http import HttpResponse

def view(request, something):
    return HttpResponse(f' {something} ')

I've found many examples like this one, but no explanation of what the 'f' means in 'HttpResponse(f' {something} '), or why is it necessary. It's not used when you don't utilize a captured value from the url.

1 Answers

Using f-string you can make dynamic strings easily.

Suppose You have variables as age = 24, name = Mateo. You want to return the string as My Name is Mateo and age is 24. For a single user, you can hardcore this program, but for multiple users, you have to go dynamically.

Here age is an integer and name is a string. Normally for making a normal sentence you have to typecast integer value to string name then you can concatenate the two strings. But in f-string you don't need to typecast any value python does it for you.

Related