,views.check_str,nam...">

How to determine a url that accepts all line strings in django

In django, I defined url as

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

But, when I introduce string inputs like ibrahim.yilmaz, ibrahi! m or ibrahim@ibrahim.com , it returns http 404. So, how can I write urls that accept everykind strings?

Any help would be appreciated.

Ibrahim

+6
source share
3 answers

Django uses regular expressions to match incoming requests. In python, a period (.) Matches any character except a newline. See docs for more details and try:

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

Also keep in mind that this will accept any character (including a slash) that might be undesirable for you. Be sure to check that everything works as you expected.

+11
source

In Django> = 2.0 you can implement as follows.

 from django.urls import path urlpatterns = [ ... path('polls/<string>/$','polls.views.detail') ... ] 
0
source

For Django 2.0


import re_path in urls.py file for example: from django.urls import re_path

then in urlpatterns write the following code:

urlpatterns = [ re_path ('prefixs /.*', your_ViewClass_name.as_view ()),]

0
source

Source: https://habr.com/ru/post/897933/


All Articles