Django PDF媒体文件不显示在我的网页上

e5nszbig  于 2023-08-08  发布在  Go
关注(0)|答案(3)|浏览(162)

我一直在网上寻找这个地方,似乎一切都在检查,也媒体图像非常好地显示
所以我有一个django web应用程序,它有一个FileField,你可以上传pdf,现在我试图显示thouse pdf,但他们得到显示错误,如下图所示。

settings.py

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')

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

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

X_FRAME_OPTIONS = 'SAMEORIGIN'

字符串

show_pdf.html

<iframe
        src="{{pdf.url}}"
    frameBorder="0"
    scrolling="auto"
    height="1200px"
    width="1200px"
></iframe>

models.py

class File_pdf(models.Model):
    title = models.CharField(max_length=50)
    pdf = models.FileField(upload_to='portfolio/pdfs')
    main_resume = models.BooleanField(default=False,help_text="set only one pdf as the main pdf for the main page")

    def __str__(self):
        return self.title

url.py

urlpatterns = [
        path('admin/', admin.site.urls),
        path('',include('portfolio.urls')),
        path('blog/',include('blog.urls'))
]

urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

views.py

def pdf_view(request,pdf_id,resume):
    dic = {}
    if resume == "yes":
        pdf_files = File_pdf.objects.filter(main_resume=True)
        pdf_file = pdf_files[0]
        dic['pdf'] = pdf_file.pdf
        dic['title'] = pdf_file.title
    else:
        pdf_files = get_object_or_404(File_pdf,pk=pdf_id)
        pdf_file = pdf_files[0]
        dic['pdf'] = pdf_file.pdf
        dic['title'] = pdf_file.title


错误如下所示:


的数据
这实际上是正确的链接

jq6vz3qz

jq6vz3qz1#

您需要重新加载当前打开的页面,并通过ctrl+shift+r覆盖本地缓存的版本。这应该对你有帮助

6qfn3psc

6qfn3psc2#

settings.py中添加以下代码:

X_FRAME_OPTIONS = 'ALLOW-FROM <your localhost URL>'

字符串
例如

X_FRAME_OPTIONS = 'ALLOW-FROM http://127.0.0.1:8000/'

66bbxpm5

66bbxpm53#

事实证明,一切都是正确的,但勇敢的缓存不知何故与X_FRAME_OPTIONS更新本身发生冲突。所以在删除brave的缓存之后就足够了:)

相关问题