How to define url which accept everykind of strings in django

10,321

Solution 1

Django uses regular expressions to match incoming requests. In python a dot (.) matches any character except a newline. See docs for more information and try:

(r'^checkstring/(?P<string>.+)/$',views.check_str,name='check str')

Also keep in mind that this will accept any character (including the forward slash) which may not be desirable for you. Be sure to test to make sure everything works as you would expect.

Solution 2

In Django >= 2.0, you can implement in the following way.

from django.urls import path

urlpatterns = [
    ...
    path('polls/<string>/$','polls.views.detail')
    ...
]
Share:
10,321
ibrahimyilmaz
Author by

ibrahimyilmaz

Updated on June 04, 2022

Comments

  • ibrahimyilmaz
    ibrahimyilmaz almost 2 years

    In django, I defined url like that

    (r'^checkstring/(?P<string>\w+)/$',views.check_str,name='check str')
    

    But, When i enter string inputs like ibrahim.yilmaz, ibrahi!m or [email protected], it returns http 404. So how can i write the url which accept everykind of string?

    any help will be appreciated.

    İbrahim