In my project with django I should do a site with flexible url. For instance:
mysite.com/register/kj-1k-32-mk
When someone write some other things after register/ I want to redirect them to my site. How can I redirect them. What should my urls.py looks like?
urls.py
from django.conf.urls import url
from django.contrib import admin
from olvapp import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^register/(?P<pk>\d )',views.guaform,name='custform'),
]
When I do like above, the part after register/ can't begin with a letter.
CodePudding user response:
That's because of the \d
pattern which only allows a sequence of digits. If you want a slug-like sequence, you can use:
from django.conf.urls import url
from django.contrib import admin
from olvapp import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^register/(?P<pk>[-\w] )',views.guaform,name='custform'),
]
Note: django-1.11 is no longer supported [Django-doc] since April 2020, you should consider upgrading the Django version.