Rendolph
Rendolph

Reputation: 431

Assign a simple HTML site with django

I want to assign/add a simple static HTML site to my django site. I know that's not what django is for but at the beginning I just want to create a simple site with a href to the other directories on the project.

For e.g.

index.html

<html>
<head></head>
<body>

    <a href="/speiseplan">anzeige</a><br />
    <a href="/speiseplanEdit">Speiseplan </a><br />
    <a href="/speiseplan">Menu1</a><br />
    <a href="/speiseplan">Menu2</a><br />
    <a href="/speiseplan">Menu3</a><br />

</body>
</html>

Now I want to add this site to urls.py. I read that it shoud be possible with TemplateView. I already tried this:

urlpatterns = [
    path('', TemplateView.as_view(template_name="/templates/speiseleitsystem/index.html")),
    path('speiseplan/', views.speiseplan_Anzeige, name='speiseplan_Anzeige'),
    path('speiseplanEdit/', views.speiseplan_Eintragen, name='speiseplan_Eintragen'),
    path('menu1/', views.speiseplan_menu1, name='speiseplan_menu1'),
    path('menu2/', views.speiseplan_Anzeige, name='speiseplan_menu2'),
    path('menu3/', views.speiseplan_Anzeige, name='speiseplan_menu3'),
]

My error code:

TemplateDoesNotExist at /
/templates/speiseleitsystem/index.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 3.1.6
Exception Type: TemplateDoesNotExist
Exception Value:    
/templates/speiseleitsystem/index.html
Exception Location: C:\Users\xxxxx\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\loader.py, line 47, in select_template
Python Executable:  C:\Users\xxxx\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.0
Python Path:    
['C:\\Users\\xxxx\\Documents\\GitHub\\speiseplan\\speiseplan',
 'C:\\Users\\xxxx\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
 'C:\\Users\\xxx\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
 'C:\\Users\\xxx\\AppData\\Local\\Programs\\Python\\Python39\\lib',
 'C:\\Users\\xxx\\AppData\\Local\\Programs\\Python\\Python39',
 'C:\\Users\\xxxx\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']
Server time:    Thu, 18 Feb 2021 15:15:06 +0000

Did I missed something?

Upvotes: 0

Views: 44

Answers (2)

Rendolph
Rendolph

Reputation: 431

In case anyone will struggle with the same problem, the solution is a mix of all answers here.

  1. Add the template directory into the setting.py inside TEMPLATES and there DIRS
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['speiseleitsystem/templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            ],
        },
    },
]`

Note: Depending on the project structure only 'templates' is needed in DIRS

  1. Add the right path to urls.py (as MoPo mentioned)
urlpatterns = [
    path('', TemplateView.as_view(template_name="speiseleitsystem/index.html")),
]

Because of the template directory in setting.py templates/... is not needed here.

Upvotes: 0

MoPo
MoPo

Reputation: 85

Depending on your project structure and settings, this might work:

In your url patterns, you have this line: path('', TemplateView.as_view(template_name="/templates/speiseleitsystem/index.html")),

Change it to :

path('', TemplateView.as_view(template_name="speiseleitsystem/index.html")),

Django already knows that it has to look inside templates folder, given that your structure is something like this:

-project
   - speiseleitsystem
       \templates
           \speiseleitsystem
                \index.html

Remove /template/ from the url pattern and django will look in the correct place. Currently it is looking for this directory: project/speiseleitsystem/templates/speiseleitsystem/templates/speiseleitsystem/index.html Which is surely not what you want it.

Upvotes: 1

Related Questions