Django how to set main page

前端 未结 4 2008
予麋鹿
予麋鹿 2021-01-07 18:38

i want to set a main page or an index page for my app. i tried adding MAIN_PAGE in settings.py and then creating a main_page view returning a main_page object, but it doesn\

4条回答
  •  温柔的废话
    2021-01-07 19:04

    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.

提交回复
热议问题