I am trying to make a portal where I take landing page template from different users and check manually that template is working file or not.
If everything looks good then I want to upload that theme from my django admin panel which create a new folder for every template like in this case its template1, template/theme/template1/index.html
folder and also same with static files I want to upload zip file for all folders of static file and what will happen in backend is it go to folder static/template1/(unzipped static files)
where unzipped static files is the location where all the static file will unzip itself so that my index.html and static files can communicate with each other.
I am not able to find any solution for this.
How can we make a model for uploading file like this to make a new folder every time?
from django.db import models
from django.contrib.auth.models import User
from django.db import models
class themes(models.Model):
index_file = models.filefield(upload_to=???????)
static_files = models.filefield(upload_to =?????)
Also I don't want to do collect static every time i upload a new template design.
Edit 2 here >> In my models.py I tried doing this but I am getting errors in it. , where I am doing wrong ?
def index_path_upload(instance, filename):
if not instance.pk:
# get count of all object, so is will be unique
number = instance.__class__.objects.count() + 1
else:
number = instance.pk
# replace filename to all object have the same
filename = "index.html"
return f"templates/theme/template{number}/{filename}"
def static_path_upload(instance, filename):
if not instance.pk:
# get count of all object, so is will be unique
number = instance.__class__.objects.count() + 1
else:
number = instance.pk
# replace filename to all object have the same
return f"static/template{number}/+.zip"
class themes(models.Model):
index_file = models.FileField(upload_to=index_path_upload)
static_file = models.FileField(upload_to=static_path_upload)
do I need to add other things in my setings file , currently this is what I have.
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
MEDIA_URL = 'static/'
MEDIA_ROOT = (os.path.join(BASE_DIR,'static/'))
why not like this ?
the upload_to
key in FileField accept a function like this
def index_path_upload(instance, filename):
if not instance.pk:
# get count of all object, so is will be unique
number = instance.__class__.objects.count() + 1
else:
number = instance.pk
# replace filename to all object have the same
filename = "index.html"
return f"template/theme/template{number}/{filename}"
the instance
params is your object ( themes models )
,the filename
params is original filename of uploaded files
so your models like
def index_path_upload(instance, filename):
...
class themes(models.Model):
index_file = models.filefield(upload_to=index_path_upload)
...
edit:
from django.conf import settings
import os
def index_path_upload(instance, filename):
if not instance.pk:
# get count of all object, so is will be unique
number = instance.__class__.objects.count() + 1
else:
number = instance.pk
# replace filename to all object have the same
filename = "index.html"
path = f"templates/theme/template{number}/{filename}"
return os.path.join(settings.BASE_DIR, path)
is will uploaded on your root directory ( where is manage.py ) + path be warning with that , is a big security flaw !