Sunday, May 3, 2020

Hey django developers, I got small issue that is hoow to specify the template_root but my loader is already specify the temppath" django.template.loaders.filesystem.Loader: C:\Users\Admin\projects\telsuko\temp\temp\file.html (Source does not exist)"

from django.shortcuts import render,redirect
from .models import upload
import zipfile as ZipFile
import os, tempfile
from django.forms import modelformset_factory
from .forms import uploadForm
from rest_framework.views import APIView
from .serializers import *
from rest_framework.response import Response 
from rest_framework import viewsets
from rest_framework import status
import zipfile
from django.views import View 
from .import serializers
from file.models import upload
from .serializers import uploadSerializers
import os 
from zipfile import ZIP_DEFLATED
from django_zipfile import TemplateZipFile
from django.shortcuts import get_list_or_404, get_object_or_404
from django.http import HttpResponse
os.chdir("C:/Users/Admin/Desktop")



def home(request):
    if request.method=="POST":
        user=request.POST['user'];
        image=request.FILES['image'];
        upload.objects.create(user=user,image=image)
        files=upload.objects.all();
        return redirect('foundation')
    else:
        return render(request,'file.html')

def foundation(request):
    files=upload.objects.all();
    return render(request,'foundation.html',{'files':files})

class download(APIView):

    def get(self,request,id):
        obj = get_object_or_404(upload, pk=id)
        context = {
            'object': obj
            }
        response = HttpResponse(content_type='application/octet-stream')
        response['Content-Disposition'] = 'attachment; filename=myfile.zip'

        container = TemplateZipFile(response, mode='w', compression=ZIP_DEFLATED, template_root= '' ) #How to specify temp root

        container.write_template('file.html', context=context)
 
        container.close()
        return response


class datalist(APIView):
    def get(self,request):
        datas=upload.objects.all()
        serializer = uploadSerializers(datas,many=True)
        return Response(serializer.data)

    def post(self,request):
        serializer=uploadSerializers(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors)

    def delete(self,request):
        serializer=upload.objects.all()
        serializer.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


class datadetail(APIView):
    def get(self,request,id):
        model=upload.objects.get(id=id)
        serializer=uploadSerializers(model)
        return Response(serializer.data)

    def delete(self,request,id):
        serializer=upload.objects.get(id=id)
        serializer.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

    






--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d5499549-4306-4356-88d3-16ca1899ff43%40googlegroups.com.

No comments:

Post a Comment