My PDF.html is not linking to any CSS file, I have tried the following:
Template PDF.HTML
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href='{% static "css/bootstrap.min.css" %}' rel="stylesheet">
<!-- Material Design Bootstrap -->
<link href='{% static "css/mdb.min.css" %}' rel="stylesheet">
<!-- Your custom styles (optional) -->
<link href='{% static "css/style.min.css" %}' rel="stylesheet">
I am using the same CSS files for other templates as base.html which is working fine and linking to them but for this particular HTML it is not linking, so I am trying to find the reason for this issue
Here is the base.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
-----------------------------------------------------
<link href='{% static "css/bootstrap.min.css" %}' rel="stylesheet">
<!-- Material Design Bootstrap -->
<link href='{% static "css/mdb.min.css" %}' rel="stylesheet">
<!-- Your custom styles (optional) -->
<link href='{% static "css/style.min.css" %}' rel="stylesheet">
I am using weazyprint to get the pdf so here is the views I have tried to use it but it is returned FileNotFoundError at /admin/order/50/pdf/ [Errno 2] No such file or directory: 'C:\\\\Users\\\\User\\\\Desktop\\\\static_rootcss/pdf.css'
@staff_member_required
def admin_order_pdf(request, order_id):
order = get_object_or_404(Order,ordered=True, id=order_id)
html = render_to_string('pdf.html', {'order': order})
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = f'filename="order_{order.id}.pdf"'
weasyprint.HTML(string=html).write_pdf(response,
stylesheets=[weasyprint.CSS(settings.STATIC_ROOT + 'css/pdf.css')])
return response
I am using the followig config in the settings.py
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_in_env')]
VENV_PATH = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Try:
weasyprint.HTML(string=html).write_pdf(response,
stylesheets=[weasyprint.CSS(settings.STATIC_ROOT + )])
It seems like settings.STATIC_ROOT
was not pointing towards the correct path. So, adding the correct path ie STATICFILES_DIRS
will work. So:
@staff_member_required
def admin_order_pdf(request, order_id):
order = get_object_or_404(Order,ordered=True, id=order_id)
html = render_to_string('pdf.html', {'order': order})
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = f'filename="order_{order.id}.pdf"'
weasyprint.HTML(string=html).write_pdf(response,
stylesheets=)
return response
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.