'How can i do my url flexible in Django 1.11?
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.
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Willem Van Onsem |
