user11173832
user11173832

Reputation:

Django finding the path of the file problem

skin
  -mysite
      -myapp
         -templates
             -index.html
         -mysite
             -urls.py
             -settings.py
         -admin.py
         -views.py

I create the virtual environment in

~/home/env 

and the index.html is located in

/home/jake/Gits/skin/mysite/myapp/templates/index.html 


views.py 
from django.shortcuts import render, render_to_response


# Create your views here.
def index(request):
    return render_to_response('index.html')


url.py

from django.conf.urls import include, url
from django.contrib import admin
from myapp import views as v
urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', v.index),
]


index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
</html>

and the this Django project did not find the right path

TemplateDoesNotExist at / index.html Request Method: GET Request
URL: http://127.0.0.1:8000/ Django Version: 1.8.1 Exception
Type: TemplateDoesNotExist Exception Value: index.html Exception
Location: /home/jake/Gits/skin/mysite/myapp/views.py in index, line 6
Python Executable: /home/jake/Gits/skin/bin/python Python
Version: 3.7.3 Python Path: ['/home/jake/Gits/skin/mysite',
'/home/jake/Gits/skin/lib/python37.zip',
'/home/jake/Gits/skin/lib/python3.7',
'/home/jake/Gits/skin/lib/python3.7/lib-dynload',
'/home/jake/anaconda3/lib/python3.7',
'/home/jake/Gits/skin/lib/python3.7/site-packages'] Server time: Wed,
8 Jan 2020 06:47:32 +0000 Template-loader postmortem Django tried
loading these templates, in this order:

Using loader django.template.loaders.filesystem.Loader: Using loader
django.template.loaders.app_directories.Loader:
/home/jake/Gits/skin/lib/python3.7/site-packages/django/contrib/admin/templates/index.html
(File does not exist)
/home/jake/Gits/skin/lib/python3.7/site-packages/django/contrib/auth/templates/index.html
(File does not exist) Traceback Switch to copy-and-paste view
/home/jake/Gits/skin/mysite/myapp/views.py in index
return render_to_response('index.html') ... ▶ Local vars

Upvotes: 0

Views: 94

Answers (2)

H-unt3R
H-unt3R

Reputation: 1

In right way your project directory structure should look like this:

YourFolder

-Yourapp

-urls.py

-settings.py

-templates

index.html

-staticfiles
-manage.py

It seems that your variable in settings.py(BASE_DIR) - home/You/Gits/skin/mysite/. There is 2 solution. 1.Change your BASE_DIR variable. 2. change your templates location.

But my advice, create project properly according to documentation.

Upvotes: 0

Amit
Amit

Reputation: 2098

With minimal change ..

TEMPLATES = [
    {
        ...., ## Your other settings
        'DIRS': [os.path.join(BASE_DIR, 'templates'),'templates'], ## Little change here
        'APP_DIRS': True,
        ...., ## Your other settings
    },
]

Please let me know if there is still an error.

Upvotes: 0

Related Questions