3

I want to change php website into django. In php, we have controller and view folder. Inside view, I have many folder in view.

View
  --- Users
        --- user1.ctp
        --- user2.ctp
  --- Layouts
        --- layout1.ctp
        --- layout2.ctp

Now I am using django this is how my directory looks

MyProject
     --- myapp
           --- views.py
           --- urls.py
           --- migrations
                  --- __pycache__
           --- static
                 --- myapp
                       --- mycss.css
           --- templates
                 --- myapp
                      --- Users
                            --- user1.html
                            --- user2.html
                      --- Layouts
                            --- layout1.html
                            --- layout2.html
     --- MyProject
     

Whenever I am putting user1.html inside /templates/myapp, I am able to render but I want to put Users related file in Users folder. I am getting

TemplateDoesNotExist at /
myapp/Layouts/layout1.html

urls.py (Inside myapp)

from django.contrib import admin
from django.urls import path
from django.urls.conf import include
from . import views,login

urlpatterns = [
    path("",views.index),
]

urls.py (Inside MyProject)

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

view.py

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return render(request,'myapp/Layouts/layout1.html')


settings.py


from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-j+_1ue45jw(f8u0utf*uh&s1$j-'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp'
]

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 = 'myproject.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',
            ],
        },
    },
]

WSGI_APPLICATION = 'python_test.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mypage',
        'USER': 'root',
        'PASSWORD': '',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


7
  • Please show us your view file Commented Aug 25, 2021 at 3:26
  • Sorry, forgot to add view.py. Now I edited. Commented Aug 25, 2021 at 3:30
  • Can you also show your TEMPLATES and INSTALLED_APPS settings? Commented Aug 25, 2021 at 3:33
  • @anonymous which template is not being found - what is the full error? Commented Aug 25, 2021 at 3:33
  • Please share your settings file Commented Aug 25, 2021 at 3:36

1 Answer 1

1

You have to configure your additional templates in settings.py like this:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMP_DIR = os.path.join(BASE_DIR, 'templates')
ADDITIONAL_TEMPLATE = os.path.join(BASE_DIR, 'path/to/template')
# add as many as you required

Use your template dirs as following:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # add your all template dirs here
        'DIRS': [TEMP_DIR, ADDITIONAL_TEMPLATE, ... ...],
        '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',
                # add your context processor here
            ],
        },
    },
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.