Home > Back-end >  Django cannot find path although it is not hardcoded
Django cannot find path although it is not hardcoded

Time:07-25

The background of this problem is that I am deploying a Django project on production server. Upon testing on production server I get the below error from my asgi.log

2022-07-24 07:03:12,962 ERROR    Internal Server Error: /paymentWebhook
Traceback (most recent call last):
  File "/home/jianwu/DimsumBox_website/env/lib/python3.10/site-packages/asgiref/sync.py", line 482, in thread_handler
    raise exc_info[1]
  File "/home/jianwu/DimsumBox_website/env/lib/python3.10/site-packages/django/core/handlers/exception.py", line 38, in inner
    response = await get_response(request)
  File "/home/jianwu/DimsumBox_website/env/lib/python3.10/site-packages/django/core/handlers/base.py", line 233, in _get_response_async
    response = await wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/jianwu/DimsumBox_website/env/lib/python3.10/site-packages/asgiref/sync.py", line 444, in __call__
    ret = await asyncio.wait_for(future, timeout=None)
  File "/usr/lib/python3.10/asyncio/tasks.py", line 408, in wait_for
    return await fut
  File "/home/jianwu/DimsumBox_website/env/lib/python3.10/site-packages/asgiref/current_thread_executor.py", line 22, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/jianwu/DimsumBox_website/env/lib/python3.10/site-packages/asgiref/sync.py", line 486, in thread_handler
    return func(*args, **kwargs)
  File "/home/jianwu/DimsumBox_website/env/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/jianwu/DimsumBox_website/env/lib/python3.10/site-packages/django/views/decorators/http.py", line 40, in inner
    return func(request, *args, **kwargs)
  File "/home/jianwu/DimsumBox_website/dimsumbox/./customer/views.py", line 124, in payment_webhook
    paymentHandle = handlePostPayment(request = request)
  File "/home/jianwu/DimsumBox_website/dimsumbox/./customer/views.py", line 165, in handlePostPayment
    pdfReceipt = webshopUtils.generateReceipt(session_id = customer.session_id)
  File "/home/jianwu/DimsumBox_website/dimsumbox/./dimsumbox/Modules/webshopUtils.py", line 484, in generateReceipt
    return pdf.pdf.output()
  File "/home/jianwu/DimsumBox_website/env/lib/python3.10/site-packages/fpdf/fpdf.py", line 2929, in output
    self.close()
  File "/home/jianwu/DimsumBox_website/env/lib/python3.10/site-packages/fpdf/fpdf.py", line 672, in close
    self._enddoc()  # close document
  File "/home/jianwu/DimsumBox_website/env/lib/python3.10/site-packages/fpdf/fpdf.py", line 3672, in _enddoc
    self._putresources()  # trace_size is performed inside
  File "/home/jianwu/DimsumBox_website/env/lib/python3.10/site-packages/fpdf/fpdf.py", line 3565, in _putresources
    self._putfonts()
  File "/home/jianwu/DimsumBox_website/env/lib/python3.10/site-packages/fpdf/fpdf.py", line 3205, in _putfonts
    ttfontstream = ttf.makeSubset(font["ttffile"], subset)
  File "/home/jianwu/DimsumBox_website/env/lib/python3.10/site-packages/fpdf/ttfonts.py", line 423, in makeSubset
    with open(file, "rb") as self.fh:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/dimsumbox/static/font/Gotham-Light.ttf'

The problem is this path '/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/dimsumbox/static/font/Gotham-Light.ttf' is from my development server on another computer. I have made sure that the path is not hardcoded.

The path is pointing to font files, and in my actual Django code they are built from settings.STATICFILES_DIRS[0] see the below code snippet. Is it possible that the variable settings.STATICFILES_DIRS is cached so that upon moving my project from development servere to production server using git, it got carried over?

def createPDFDocument():
    '''Creates the PDF document object and adds the font to the document'''
    pdf = PDF()
    #add fonts
    pdf.pdf.add_font(family = 'hdFontLight', 
    fname = settings.STATICFILES_DIRS[0]   '/font/Gotham-Light.ttf',
    uni = True)

    pdf.pdf.add_font(family = 'hdFontMedium',
    fname = settings.STATICFILES_DIRS[0]   '/font/Gotham Medium.ttf', 
    uni = True)
    
    return pdf

Snippet from my settings.py

BASE_DIR = Path(__file__).resolve().parent.parent
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static')]

CodePudding user response:

Did you run collecstatic on your production server ?

You can read those documentation pages : collectstatic (Collects the static files into STATIC_ROOT) and STATICFILES_DIR settings.

CodePudding user response:

Turns out that this issue is not caused by settings.STATICFILES_DIR. I find out this by implementing the log and I can verify that the path to font file given to fpdf2 library was indeed correct. I am sure where the actual problem was, but I ended uninstalling fpdf2 and reinstalled it again and the issue was solved. I therefore think that the issue must be somewhere in the fpdf2 library.

  • Related