Patro Levis
Patro Levis

Reputation: 67

TemplateDoesNotExist at / index.html Django

I'm learning django when create templates so got an error

this is the error i got

urls.py

urlpatterns = [
    url(r'^$',views.index,name='index'),
    url(r'^first_app/',include('first_app.urls')),
    url(r'^admin/', admin.site.urls),
]

settings.py

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
STATIC_DIR = os.path.join(BASE_DIR, "static")

Upvotes: 0

Views: 2926

Answers (2)

The Sun
The Sun

Reputation: 61

For me it's working i am using django 3.2 in ubuntu 18.4

@@ In TEMPLATES SECTION

'DIRS': ["templates"],

@@In static section add 2nd line

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

Upvotes: 1

Dennis Hartel
Dennis Hartel

Reputation: 56

As you are new to django and possibly following older tutorials there is a better readable url routing syntax for django versions > 2.0. You would use path() instead of url(). Check out the official documentation (https://docs.djangoproject.com/en/3.1/releases/2.0/).

Your error occurs because you are trying to set the url routing for your views in your project folder rather than in your app folder. That means that you need to move your index routing to the urls.py file in your app folder.

In your project folder under urls.py:

urlpatterns = [
    path('first_app', include('first_app.urls')),
    path('admin/', admin.site.urls),
]

In your app folder under urls.py :

urlpatterns = [
   path('index', views.index, name='index')
]

Note: your index.html is below .../first_app/index

Upvotes: 0

Related Questions