pythondjangopath

Python; Django adding characters to path name causing OS error 22


I've been trying to get django to render a template I created. At first it said that the template does not exist, however once I fixed the error it is now adding characters to the path and not finding the template because of that.

The path should be: C:\\Users\\ABC\\Desktop\\science_crowd\\Lightweight_Django\\placeholder\\home.html

However the error says that it cannot find: C:\\Users\\Benjamin\\Desktop\\science_crowd\\Lightweight_Django\\placeholder\\:\\home.html

It added a colon and another backslash for no reason.

The settings for this project is as follows:

ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '127.0.0.1').split(',')

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

settings.configure(
    DEBUG = DEBUG,
    SECRET_KEY = SECRET_KEY,
    ALLOWED_HOSTS = ALLOWED_HOSTS,
    ROOT_URLCONF = __name__,
    MIDDLEWARE_CLASSES = (
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ),
    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles'
    ),
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': os.path.join(BASE_DIR, 'templates'),
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    "django.contrib.auth.context_processors.auth",
                ],
            },
        },
    ],
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    ),
    STATIC_URL = '/static/',
)

The view that is trying to render the template:

def index(request):
    example = reverse('placeholder', kwargs = {'width': 50, 'height': 50})
    context = {
        'example': request.build_absolute_uri(example)
    }
    dir = os.path.join(BASE_DIR, 'templates')
    return render(request, 'home.html', context = context)

Thank you so much for your help!


Solution

  • The DIRS settings expects a list or tuple. Try:

    'DIRS': [os.path.join(BASE_DIR, 'templates')],