django-modelsdjango-templatespython-django-storages

How to upload templates and static files to a django app?


I am trying to upload webpages to my django server. They are all project of mine, and I want to be able to add more projects in the future through the admin panel:

I am working in an app called projects

This is the model I am using:

from django.db import models
from django.utils.timezone import now
from django.core.files.storage import FileSystemStorage


# Create your models here.
class Project(models.Model):
    class ProjectType(models.TextChoices):
        PYTHON = 'Python'
        JAVASCRIPT = 'Javascript'
        REACTJS = 'React.js'
        REACTNATIVE = 'React Native'
        JAVA = 'Java'
        C = 'C'
        CPP = 'C++'

    def upload_location_photo(instance, filename):
        return f'photos/projects/{instance.slug}/{filename}'

    def upload_location_template(instance, filename):
        #I want to get into, app: projects, folder: templates/projects
        return f'projects/templates/projects/{instance.slug}/{filename}'

    def upload_location_static(instance, filename):
        #I want to get into, app: projects, folder: static/projects
        return f'projects/static/projects/{instance.slug}/{filename}'

    slug = models.CharField(max_length=200, unique=True)
    project_type = models.CharField(max_length=50, choices=ProjectType.choices, default=ProjectType.JAVASCRIPT)
    title = models.CharField(max_length=150)
    description = models.TextField(blank=True)
    date_completed = models.DateTimeField(default=now, blank=True)
    photo_main = models.ImageField(upload_to=upload_location_photo)
    photo_1 = models.ImageField(upload_to=upload_location_photo, blank=True)
    photo_2 = models.ImageField(upload_to=upload_location_photo, blank=True)
    photo_3 = models.ImageField(upload_to=upload_location_photo, blank=True)
    #FILE UPLOAD OF JS APPS
    file_html = models.FileField(upload_to=upload_location_template, max_length=100, blank=True)
    file_css = models.FileField(upload_to=upload_location_static, max_length=100, blank=True)
    file_js = models.FileField(upload_to=upload_location_static, max_length=100, blank=True)

This is in a django app called projects. This issue is, that the html, css, and js files are being uploaded into: media/projects/static/projects and media/projects/templates/projects instead of going into my app, they are being saved in the global media folder, how can I stop this, and direct them into my app's template and static folder?


Solution

  • Sorry, I asked that question too soon, however now I can help someone else hopefully!

    I needed to add a few lines of code: new imports:

    import os
    from django.core.files.storage import FileSystemStorage
    from django.conf import settings
    

    Adjust my upload location functions:

        def upload_location_template(instance, filename):
            return f'{instance.slug}/{filename}'
    
        def upload_location_static(instance, filename):
            return f'{instance.slug}/{filename}'
    

    Create new storage locations, and add them as arguments to my FileFields:

        template_storage = FileSystemStorage(location=os.path.join(settings.BASE_DIR, 'projects/templates/projects/'))
        static_storage = FileSystemStorage(location=os.path.join(settings.BASE_DIR, 'projects/static/projects/'))
        file_html = models.FileField(upload_to=upload_location_template, storage=template_storage, max_length=100, blank=True)
        file_css = models.FileField(upload_to=upload_location_static, storage=static_storage, max_length=100, blank=True)
        file_js = models.FileField(upload_to=upload_location_static, storage=static_storage, max_length=100, blank=True)
    

    It is now working perfectly, used this answer here: https://helperbyte.com/questions/177113/django-multiple-media-root