Django 2: TemplateDoesNotExist even if files placed properly - python

I have encountered interesting problem on Django 2.
In my settings.py I have written this:
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',
],
},
},
]
After that I have created folder templates in my projects root folder.
Then I have created app with name front and added it into my settings.py INSTALLED_APPS:
Inside my views.py I have added this view:
def index(request):
return render(request, 'front/index')
I have assigned its url like this:
url(r'^$', views.index, name='index'),
When I'm trying to access to this view I get this error:
TemplateDoesNotExist at /
front/index
Why this happening? Did I miss something?

Change
def index(request):
return render(request, 'front/index')
to
def index(request):
return render(request, 'front/index.html')

Related

I have problem with cutomizing the 404 page in django

This is my settings.py:
DEBUG = False
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
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',
],
},
},
]
main urls.py:
handler404 = 'home.views.custom_page_not_found_view'
my views for 404 page:
def custom_page_not_found_view(request, exception):
return render(request, "404.html", {}, status=404)
i tryed this solution:(Correct way of implementing custom 404 page in Django 3 in production)
but it doesn't work and kept getting server 500 error responses
how do i fix this?
This error was because I had used the extends tag inside the 404.html file

Django: Project name is duplicated in the template path

My project structure is roughly as follows:
dinnerproject/
dinnerproject/
settings.py
dinners/
templates/dinners/
main.html
templates/
base.html
manage.py
In settings.py I've got TEMPLATE configured like so:
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',
],
},
},
]
And dinners app is added to INSTALLED_APPS.
I use a TemplateView with template_name = "main.html", which extends base.html.
But when I try to open the page that's supposed to return main.html, I keep getting a TemplateDoesNotExist error saying:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: C:\Users\User\dinnerproject\dinnerproject\templates\main.html (Source does not exist)
(...)
django.template.loaders.app_directories.Loader: C:\Users\User\dinnerproject\dinnerproject\dinners\templates\main.html (Source does not exist)
For some reason, my project name is duplicated in the paths, so django cannot find the right directories. What am I doing wrong?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'dinnerproject','templates'),
os.path.join(BASE_DIR,'dinners','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',
],
},
},
]
urls.py:
app_name="your app name"
This will solve your error

How to use template from project in another app?

I'm thinking in organizing all my templates in inside my project: scolarte.
As suggested by this question:
What is the best location to put templates in django project?
If you can’t think of an obvious place to put your templates, we
recommend creating a templates directory within your Django project
(i.e., within the mysite directory you created in Chapter 2, if you’ve
been following along with our examples).
But I need to call it from another app. The view is called but getting error:
TemplateDoesNotExist at /cuentas/ingreso/
scolarte/templates/scolarte/registration/signup.html
I even tried to put the full path to the template in project folder:
roles/views.py:
class SignUpView(TemplateView):
template_name = 'scolarte/templates/scolarte/registration/signup.html'
# don't work neither
#template_name = 'templates/scolarte/registration/signup.html'
#template_name = 'scolarte/registration/signup.html'
#template_name = 'registration/signup.html'
roles/urls.py:
from django.urls import include, path
from .views import SignUpView, SellerSignUpView, ClientSignUpView
urlpatterns = [
path('ingreso/', SignUpView.as_view(), name='signup'),
]
scolarte/urls.py
urlpatterns = [
path('', include('core.urls')),
path('cuentas/', include('roles.urls')),
path('admin/', admin.site.urls),
]
My app is orgnized like this:
roles
|_migrations
|_templates
...
|_urls.py
|_views.py
scolarte #project name
|_templates
|_scolarte
|_registration
|_signup.html
|_setting.py
|_urls.py
setting.py
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',
],
},
},
]
UPDATE 1:
roles app:
** roles app - view.py **:
UPDATE 2:
Your path to the template is incorrect by standard Django convention, but let me show you first how to fix it. What you'll want to do is make sure in settings.py you have these settings made. This is from a Django 3.0 fresh project creation.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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',
],
},
},
]
If you use those, and follow the suggested directory structure of:
scolarte
|_scolarte
|_settings.py
|_urls.py
(etc..)
|_templates
|_scolarte
|_registration
|_signup.html
Then you can use with this path:
template_name = 'scolarte/registration/signup.html'

TemplateDoesNotExist Error in Pythonanywhere

Erorr
I wanna know where django search for index.html
Setting.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['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',
],
},
},
]
views.py
def index(request):
return render(request, 'index.html', {})
Django will look in the templates directory, that's if you created a folder called templates and if you have a file named index.html in the templates folder. For some reason I feel like the reason why you are asking this question is because you can not get your template to render when you run the server, am I correct? Please get back to me and I can do my best to fill you in so you can get your project working.
You can easily solve this problem: if the name of your project directory, where manage.py is in, is "RalphPortfolio", you need to make the following correction in settings.py:
'DIRS': ['RalphPortfolio/templates'],
add the full path to the template dirs
for example if your path is /home/some_url/your_project/templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/home/some_url/your_project_name/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',
],
},
},
]
it's because the path is not clear well and it will not find it.

django view render to template from another app

I'm trying to render to a template from another app but not sure how to import or if I should be importing with templates?
The structure is
project->
main app->
templates->
pages->
home.html
account ->
current app->
views.py
my views.py file sits within the current app and is trying to access templates within the main app (in the pages subfolder).
How would I render to it:
def temp_view(request):
....
return render(request, "home.html",context)
At first you should have most probably configure your templates static path in settings.py with something similar to this
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',
'django.template.context_processors.i18n',
],
},
},
]
APP_DIRS=True means Django will look for templates in each app directory in your case main_app/ and current_app/:
you have simply to mention the template path considering this as root path, so simply:
def temp_view(request):
....
return render(request, "pages/home.html",context)
Django will look under main_app/ directory to find the file pages/home.html
Would it be simpler to create the view in the same app you would like to render?
from mainapp.models import mainappmodel
def currentappview(request):
all = mainappmodel.objects.all()
return render(request, "pages/home.html", {'all': all})

Categories