Keith Keith - 1 year ago 88
Python Question entry for django v1.10

I'm having trouble getting the url entry in my app's file how I want it.
When a person enters
, I want it to use the same view as
I can use
url(r'^index.html$', views.index, name='index'),
and the view is properly displayed when I type
into the chrome address bar, or I can trim the
off both the entry and the address bar and it's ok, but I figured with regex, I could get this to display for
or for

lots of googling hasn't let me to an answer yet...

update based on @moses:

from django.conf import settings
from django.conf.urls import url
from django.conf.urls.static import static
from . import views

urlpatterns = [
url(r'^(index\.?[html]{,4})?$', views.index, name='index'),
# url(r'^index$', views.index, name='index'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

now I get: "Exception Value:index() takes exactly 1 argument (2 given)"

Answer Source

You can use the following regex pattern that matches 0 or 1 repetitions of 'index.html' in the url i.e. either ^$ or r'^index.html$':

>>> re.match(r'^(?:index\.html)?$', 'index.html')
<_sre.SRE_Match object; span=(0, 10), match='index.html'>
>>> re.match(r'^(?:index\.html)?$', '')
<_sre.SRE_Match object; span=(0, 0), match=''>
>>> re.match(r'^(?:index\.html)?$', 'login')

And your Django url pattern becomes:

url(r'^(?:index.html)?$', views.index, name='index')

See demo:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download