Django serve static index.html with view at '/' url

后端 未结 5 2033
面向向阳花
面向向阳花 2020-12-25 11:14

I have my index.html in /static/ folder. My django app is running ok when i try:

http://127.0.0.1:8000/index.html

But i want to acces index

5条回答
  •  孤独总比滥情好
    2020-12-25 11:41

    you can create templates directory, put the html there and then render it from views.py

        def index(request):
            return render(request, 'my_app/index.html', context={})
    

    dont forget to set the templates_dir in the settings.py

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    TEMPLATES_DIR = os.path.join(BASE_DIR, "templates")
    
    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATES_DIR,],
        'APP_DIRS': True,
        ...
    

提交回复
热议问题