13👍
✅
If you want to refer to a static page (not have it go through any dynamic processing), you can use the direct_to_template
view function from django.views.generic.simple
. In your URL conf:
from django.views.generic.simple import direct_to_template
urlpatterns += patterns("",
(r"^$", direct_to_template, {"template": "index.html"})
)
(Assuming index.html
is at the root of one of your template directories.)
20👍
The new preferred way of doing this would be to use the TemplateView
class. See this SO answer if you would like to move from direct_to_template
.
In your main urls.py
file:
from django.conf.urls import url
from django.contrib import admin
from django.views.generic.base import TemplateView
urlpatterns = [
url(r'^admin/', admin.site.urls),
# the regex ^$ matches empty
url(r'^$', TemplateView.as_view(template_name='static_pages/index.html'),
name='home'),
]
Note, I choose to put any static pages linke index.html
in its own directory static_pages/
within the templates/
directory.
- [Django]-Django, ImportError: cannot import name Celery, possible circular import?
- [Django]-Use different serializers for input and output from a service
- [Django]-Models.py getting huge, what is the best way to break it up?
10👍
In case someone searching for an updated version of the answer..
from django.urls import re_path
from . import views
urlpatterns = [
re_path(r'^$', views.index, name='index')
]
and in your views.py
def index(req):
return render(req, 'myApp/index.html')
- [Django]-How to spread django unit tests over multiple files?
- [Django]-How to set or get a cookie value in django
- [Django]-Count number of records by date in Django
1👍
You could use the generic direct_to_template
view function:
# in your urls.py ...
...
url(r'^faq/$',
'django.views.generic.simple.direct_to_template',
{ 'template': 'faq.html' }, name='faq'),
...
👤miku
- [Django]-ImportError: No module named '_sqlite3' in python3.3
- [Django]-Form with CheckboxSelectMultiple doesn't validate
- [Django]-How do I access the request object or any other variable in a form's clean() method?
Source:stackexchange.com