Reputation: 797
I'm having trouble getting the url entry in my app's urls.py file how I want it.
When a person enters mysite.com:8000/
, I want it to use the same view as mysite.com:8000/index.html
I can use url(r'^index.html$', views.index, name='index'),
and the view is properly displayed when I type mysite.com:8000/index.html
into the chrome address bar, or I can trim the .html
off both the urls.py entry and the address bar and it's ok, but I figured with regex, I could get this to display for /
or for index*
lots of googling hasn't let me to an answer yet...
EDIT: 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)"
Upvotes: 1
Views: 87
Reputation: 78564
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: https://regex101.com/r/MVCPDN/2
Upvotes: 1