Manoj Tolagekar
Manoj Tolagekar

Reputation: 1960

error Template does not exist, but template is exist

Templates does not exit, but template is already exist. In this project, I am doing upload file. Please help to solve this problem.

urls.py:

from django.contrib import admin
from django.urls import path
from usermaster import views
from django.conf.urls.static import static
from mysite5 import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('upload/', views.index),
]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

views.py:

from django.http.response import HttpResponse
from django.shortcuts import render
from usermaster.forms import MachineForm

# Create your views here.
def index(request):  
    if request.method == 'POST':  
        form = MachineForm(request.POST, request.FILES)  
        if form.is_valid():  
            handle_uploaded_file(request.FILES['upload_file'])  
            model_instance = form.save(commit=False)
            model_instance.save()
            return HttpResponse("File uploaded successfuly")  
    else:  
        form = MachineForm()  
        return render(request,'usermaster/upload_file.html',{'form':form})

def handle_uploaded_file(f):  
    with open('usermaster/static/upload/'+f.name, 'wb+') as destination:  
        for chunk in f.chunks():  
            destination.write(chunk)

models.py:

from django.db import models

# Create your models here.
class Machine(models.Model):
    machine_name = models.CharField(max_length=200)
    operation_no = models.IntegerField()
    upload_file = models.FileField()

forms.py:

from django import forms
from usermaster.models import Machine


class MachineForm(forms.ModelForm):  
    class Meta:  
        model = Machine  
        fields = '__all__'

upload_file.html:

<html>
<head>
<title>Django File Upload</title>
</head>
<body>
<p><h1>Django File Upload</h1></p>
<form method="post" class="post-form" enctype="multipart/form-data">  
        {% csrf_token %}  
        {{ form.as_p }}  
        <button type="submit" class="save btn btn-default">Save</button>  
</form>  
</body>  
</html>

Templates does not exit, but template is already exist. In this project, I am doing upload file. I don't know why template does not exist giving. Please help to solve this problem.

settings.py:

MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR,'media')
STATIC_URL = 'static/'
STATIC_ROOT=os.path.join(BASE_DIR,'static')

usermaster directory:

templates/usermaster/upload_file.html

Error:

TemplateDoesNotExist at /upload/
usermaster/upload_file.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/upload/
Django Version: 4.0
Exception Type: TemplateDoesNotExist
Exception Value:    
usermaster/upload_file.html
Exception Location: C:\Users\Manoj\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\loader.py, line 19, in get_template
Python Executable:  C:\Users\Manoj\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.5
Python Path:    
['C:\\Users\\Manoj\\Desktop\\sample\\mysite5',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\win32',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\win32\\lib',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\Pythonwin']
Server time:    Wed, 05 Jan 2022 05:24:19 +0000

Upvotes: 0

Views: 1044

Answers (2)

Akash Nagtilak
Akash Nagtilak

Reputation: 325

Please go into settings.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],# Please add this in settings Templates
        'APP_DIRS': True,
        'OPTIONS': {
            'libraries':{
                'core': 'coder.templatetags.core',
            },
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.media',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Add This line in settings.py in TEMPLATES 'DIRS': [os.path.join(BASE_DIR, 'templates')],

for reference use this.

Thank You!!!

Upvotes: 0

Tasnuva Leeya
Tasnuva Leeya

Reputation: 2795

If your templates folder in base dir, make sure you have added the directory in settings.py file.

Sample example:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'project_townhall/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',
            ],
        },
    },
]

here project_townhall is my project name. Optionally you can add the directory like [os.path.join(BASE_DIR, 'templates')] instead of hard coded name.

Upvotes: 0

Related Questions