0

I've a trouble with django and I don't know how to fix it.

I use DJANGO 3.2.8 and Python 3.7.12

I code an app that allow a user to upload some file into my DB and when an admin are login he can select an user and clic on ID then it's display the user's ID. When I turn DEBUG = True, everything is ok but when I turn DEBUG = False, I've an 404 error when I try to display the user's ID and I don't know how to fix, I try many thing but nothing work (I use collectstatic for the CSS and it's work when DEBUG = FALSE I've got all my pictures ...)

My ID are save in media folder.

Setting

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

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

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles') 
STATIC_URL = 'staticfiles/'

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

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

Model

def path_and_rename(path, prefix):
    def wrapper(instance, filename):
        ext = filename.split('.')[-1]
        project = "pid_%s" % (instance.name,)
        # get filename
        if instance.pk:
            complaint_id = "cid_%s" % (uuid.uuid4().hex,)
            filename = '{}.{}.{}.{}'.format(prefix, project, complaint_id, ext)
        else:
            # set filename as random string
            random_id = "rid_%s" % (uuid.uuid4().hex,)
            filename = '{}.{}.{}.{}'.format(prefix, project, random_id, ext)
            # return the whole path to the file
        return os.path.join(path, filename)
    return wrapper


class Company(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)
    id_card = models.FileField(upload_to=path_and_rename("id_card/", 'id_card'), null=True)

Template

<div>
ID : <a href="{{ companys.id_card.url }}">click here</a>
</div>

Thanks !

tomferrari
  • 53
  • 4
  • Try this!: https://stackoverflow.com/questions/44555187/django-media-files-not-showing-with-debug-false-on-production-django-1-10 ...I'm surprised anything works with `Debug=False`, that normally breaks all the static files unless it's in a prod env – Nealium Sep 08 '22 at 01:54
  • Does this answer your question? [Django give Error 500 for all static files like CSS and Images, when DEBUG is False](https://stackoverflow.com/questions/61610680/django-give-error-500-for-all-static-files-like-css-and-images-when-debug-is-fa) – Ivan Starostin Sep 08 '22 at 06:55
  • No its not work for me. For the CSS I use colletstatic and its work, but when the user upload file I see that I can edit Nginx config but I don't know where I can found this config – tomferrari Sep 08 '22 at 09:50

1 Answers1

0

I finally find a solution.

I create a sub-domain name media.mywebsite.com

Then I update my settings.py

 MEDIA_ROOT = "/home/USER/public_html/media.mywebsite.com/"

 MEDIA_URL = "https://media.mywebsite.com/"

And it's work ! Maybe not the best way but it's work

Thanks

tomferrari
  • 53
  • 4