0

I'm currently learning regex patterns, using DJango 1.1. More specifically, I am trying to display whatever number is entered into the url using the '\d+' pattern. I'm brand new to DJango, so I hope that description wasn't too vague. Any help would be greatly appreciated.

My urls.py url pattern is:

url(r'^(?P<number>\d+)$', views.show)

My views.py method is:

def show(request, number):
    return HttpResponse("placeholder to display blog number: {number}")

I don't quite understand what i'm missing.

If I enter the number 20 into my url: localhost:0000/20, I'm expecting to see an output of:

placeholder to display number: 20

But instead, I get an output of:

placeholder to display number: {number}
  • Possible duplicate of [Is there a Python equivalent to Ruby's string interpolation?](https://stackoverflow.com/questions/4450592/is-there-a-python-equivalent-to-rubys-string-interpolation) – 41686d6564 stands w. Palestine May 26 '19 at 08:54

2 Answers2

3

Update HttpResponse to:

return HttpResponse(f"placeholder to display blog number: {number}")

More information can be found on documentation regarding formatted string literals.

FYI: this solution will work for python 3.6+, for other python versions you can format the string in following way:

return HttpResponse(
    "placeholder to display blog number: {number}".format(number=number)
)
ruddra
  • 50,746
  • 7
  • 78
  • 101
1

You forgot to format the response. It should be like,

return HttpResponse(f"placeholder to display blog number: {number}")

In python f"strings" are used for integrating a value into a string

DankCoder
  • 369
  • 1
  • 4
  • 15