开发者

How to define url which accept everykind of strings in django

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, ibr开发者_StackOverflowahi!m or ibrahim@ibrahim.com, it returns http 404. So how can i write the url which accept everykind of string?

any help will be appreciated.

İbrahim


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.


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

from django.urls import path

urlpatterns = [
    ...
    path('polls/<string>/$','polls.views.detail')
    ...
]


For Django 2.0


import re_path in urls.py file like this:
from django.urls import re_path

then in the urlpatterns write the following code:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜