简体   繁体   中英

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")

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/')

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM