django url pattern for

后端 未结 4 2002
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 14:26

In Django what is the url pattern I need to use to handle urlencode characters such as %20

I am using (?P[\\w]+) but this only handles alpha

相关标签:
4条回答
  • 2020-12-05 14:36

    I am using Django 2.2.

    It handles the %20 (which means space) in url using Path converter: str

    You simply need to use:

    <name> or <str:name>
    

    For example, following example loads view "some_view" defined in view.py

    #urls.py
    from django.urls import path
    from . import views
    urlpatterns = [
       path("<name>",views.some_view),
       ....
    ]
    

    The following function renders "some.html" after processing. In this example sending the received name to "some.html".

    #view.py
    def some_view(request, name):
        # process here
        context = { "name" : name }
        return render(request,"some.html",context)
    
    0 讨论(0)
  • 2020-12-05 14:51

    The best way to do that and allow others chars is using '\s' that is any spaces, tabs and new lines

    (?P<name>[\w\s]+)
    
    0 讨论(0)
  • 2020-12-05 14:54

    If you only want to allow space:

    (?P<name>[\w\ ]+)
    
    0 讨论(0)
  • 2020-12-05 14:55

    I was able to make it work using the configuration given below. Check if it will suit your needs.

    (?P<name>[\w|\W]+)
    
    0 讨论(0)
提交回复
热议问题