Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

how can I grab a number from URL and check it's length in django?

I need to verify the length of number grabbed from URL such as example.com/12345678 in django ?

urlpatterns = [
    path("", main.views.index, name="index"),
    path("admin/", admin.site.urls),
    path('<int:tnum>/', main.views.number, name="number")

And if the number doesn't match a certain length I want to output number is invalid.

question from:https://stackoverflow.com/questions/66049395/how-can-i-grab-a-number-from-url-and-check-its-length-in-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This has already been answered here: How to find length of digits in an integer?

Essentially, you first convert the integer to a string and then get the length of the string.

So in your view, you will need to have some logic like this:

tnum_length = len(str(tnum))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...