0

I'm trying to simply give an app url an option of /headless/ to make it show a different template.

My project/urls.py has:

urlpatterns = [
    url(r'^datastore/', include('datastore.urls')),
]

My app/urls.py has:

app_name = 'datastore'
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^datastore/(?P<headless>"headless"{1})/$', views.index,name='index'),
]

I'm getting a 404 error with the above.

I've also tried:

url(r'^datastore/(?P<headless>"headless"?)/$',
url(r'^datastore/(?P<headless>\w{1})/$', views.index, name='index'),
url(r'^datastore/(?P<headless>\w+)/$', views.index, name='index'),
nickie
  • 5,608
  • 2
  • 23
  • 37
Simply Seth
  • 3,246
  • 17
  • 51
  • 77

2 Answers2

2

You have to remove the prefix /datastore/ from the app urlpattern:

app_name = 'datastore'
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<headless>"headless"{1})/$', views.index,name='index'),
]

According to Django's documentation:

Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.

So, the url pattern in your project's settings consumes the datastore/ prefix. You should be able to check that this is so by trying /datastore/datastore/headless/ (this should work with your current configuration).

Notice, however, that the combination of regular expression matches either /datastore/headless/ or /datastore// (the same in all your variations). This may not be what you want. Wilfried's answer (which does not address the real issue here) shows you how to better do what I think you intend to.

nickie
  • 5,608
  • 2
  • 23
  • 37
0

It's may be your regex on your url.

If you need access to url :

  • /datastore/

  • /datastore/headless/

you can create two url, pointing to the same view.

urlpatterns = [
   url(r'^$', views.index, name='index'),
   url(r'^datastore/$', views.index, name='index'),
   url(r'^datastore/(?P<headless>(headless))/$', views.index, name='index'),
]

If you want, it's not necessary to use a parameter. If you have only headless like possibility.

urlpatterns = [
   url(r'^$', views.index, name='index'),
   url(r'^datastore/$', views.index, name='index'),
   url(r'^datastore/headless/$', views.index, name='index'),
]
Wilfried
  • 1,623
  • 1
  • 12
  • 19
  • 1
    This is useful for getting rid of the double `/` required by the original regular expression. However, it does not provide the `index` entry point with any information about whether `headless` was present, or not. – nickie Dec 29 '16 at 16:52