When I click on create post it takes me to .../blog/create/
and I get this error: TemplateDoesNotExist at /blog/create/
base.html
<!DOCTYPE html>
<html lang*en>
<head>
<title>This is the Title</title>
{% include 'snippets/header.html' %}
</head>
<body>
<!-- Body -->
<style type="text/css">
.main{
min-height: 100vh;
height: 100%;
}
</style>
<div class="main">
{% block content %}
{% endblock content %}
</div>
{% include 'snippets/footer.html' %}
</body>
</html>
blog/Template/blog/create.html
{% extends 'base.html' %}
{% block content %}
<p>Create a new blog...</p>
{% endblock content %}
blog/urls.py
from django.urls import path
from blog.views import(
create_blog_view,
)
app_name = 'blog'
urlpatterns = [
path('create/', create_blog_view, name="create"),
]
main urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from personal.views import (
home_screen_view,
)
from account.views import (
registration_view,
logout_view,
login_view,
account_view,
)
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_screen_view, name="home"),
path('register/', registration_view, name="register"),
path('blog/', include('blog.urls', 'blog')),
path('logout/', logout_view, name="logout"),
path('login/', login_view, name="login"),
path('account/', account_view, name="account"),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
blog/views.py
from django.shortcuts import render
from blog.models import BlogPost
def create_blog_view(request):
return render(request, "blog/create.html", {})
personal/Template/snippets/home.html
{% extends 'base.html' %}
{% block content %}
<style type="text/css">
@media (max-width: 768px) {
.right-column{
margin-left: 0px;
}
}
@media (min-width: 768px) {
.right-column{
margin-left: 20px;
}
}
.blog-post-container{
background-color: #fff;
margin-bottom: 20px;
width: 100%;
}
.create-post-bar{
background-color: #fff;
margin-bottom:20px;
}
.left-column{
padding:0px;
}
.right-column{
padding:0px;
}
</style>
<div class="container">
<div class="row">
<div class="create-post-bar d-lg-none col-lg-7 offset-lg-1">
<a href="{% url 'blog:create' %}">Create post</a>
</div>
<div class="left-column col-lg-7 offset-lg-1">
<div class="blog-post-container">
<p>Thingy</p>
</div>
<div class="blog-post-container">
<p>Thingy</p>
</div>
<div class="blog-post-container">
<p>Thingy</p>
</div>
</div>
<div class="right-column col-lg-3 d-lg-flex d-none flex-column">
<div class="create-post-bar">
<p>Stuff</p>
<p>Stuff</p>
<p>Stuff</p>
<a class="p-2 btn btn-outline-primary" href="{% url 'blog:create' %}">Create post</a>
</div>
</div>
</div>
</div>
{% endblock content %}
settings.py
from pathlib import Path
import os
BASE_DIR = Path(__file__).resolve().parent.parent
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'personal',
'account',
'blog',
#Djangos apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'mysite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'Templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
AUTH_USER_MODEL = 'account.Account'
WSGI_APPLICATION = 'mysite.wsgi.application'
...
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
os.path.join(BASE_DIR, 'media'),
]
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media_cdn')
I was expecting it to redirect me to the new page but it shows an error.
3
Answers
Apparently your app doesn’t look into
blog
‘s template folder. It only looks intopersonal
and
account
‘s template folders. You can check that in the attached picture undertemplate-loader postmortem
. It usually means that the app is not installed properly.There are lots of reasons for such a thing, among them:
./src/
. It seems that it’s not the case as theurl.py
is working properly.settings.py
after you added theblog
app.Otherwise you might have changed the app’s template directory somewhere.
You have, in your settings.py, the configuration
so you need to have the Templates directory in your base directory, not inside each app, it should be
and not
With APP_DIRS enabled Django searches for templates inside each app
/templates/
subfolder. Lowercase, plural.You have
Template
– wrong case, missings
in the end. So yes, the template does not exist for Django because it cannot be found at any expected location.