Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 模板文件在django中重写媒体文件_Python_Django - Fatal编程技术网

Python 模板文件在django中重写媒体文件

Python 模板文件在django中重写媒体文件,python,django,Python,Django,我有vue应用程序和django应用程序。我单独构建它们,并使用vue的index.html作为模板 当我转到图像目录时。例如:http://localhost:8000/media/posts/images/image.png。我得到的不是图像,而是一个静态文件。它发生在开发和生产环境上 这是我的url.py文件模板视图响应根URL。我使用index.html作为模板。它来自vue构建 from django.conf import settings from django.conf.url

我有vue应用程序和django应用程序。我单独构建它们,并使用vue的
index.html
作为模板

当我转到图像目录时。例如:
http://localhost:8000/media/posts/images/image.png
。我得到的不是图像,而是一个静态文件。它发生在开发和生产环境上

这是我的
url.py
文件<代码>模板视图响应根URL。我使用
index.html
作为模板。它来自vue构建

from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, re_path
from django.views.generic import TemplateView

urlpatterns = [
    path('api/admin/', admin.site.urls),
    ## I use index.html as template. It comes from vue dist directory
    re_path('', TemplateView.as_view(template_name='index.html'))
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我在静态文件目录中包含了vue构建文件

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

STATICFILES_DIRS = ['/home/sam/Documents/Projects/vue/flow/dist/static']

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
我使用vue build中的
index.html
作为模板

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['/home/sam/Documents/Projects/vue/flow/dist'],
        ...
    },
]
我尝试了不同的方法来修复它

  • 如果我从
    urlpatterns
    中删除
    re\u路径(“”,TemplateView.as\u视图(template\u name='index.html'))
    行,图像问题就会消失。然而,我错过了静态文件

  • 我尝试使用
    path
    而不是
    re\u path
    。如下所示:
    path(“”,TemplateView.as_-view(template_-name='index.html'))
    。在本例中,我同时获得图像和静态文件。但是在用
    Ctrl+R
    刷新静态页面之后,我得到了404 Django默认页面


  • 问题是如何获得图像和静态文件而不出现上述刷新问题?

    我在
    媒体
    URL之后添加了
    模板视图
    。它很好用

    urlpatterns = [
        path('admin/', admin.site.urls)
    ]
    
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += [re_path('', TemplateView.as_view(template_name='index.html'))]
    
    `urlpatterns=[path('api/admin/',admin.site.url)]

    urlpatterns+=静态(settings.MEDIA\u URL,document\u root=settings.MEDIA\u root)
    urlpatterns+=static(settings.static\u URL,document\u root=settings.static\u root)`

    您的索引页在某种程度上重写了静态/媒体文件。您可以保留默认的
    path(“”,TemplateView.as_view(template_name='index.html'))
    url,并添加类似于此规则的
    re_path('^((?!media\/))*$',TemplateView.as_view(template_name='index.html'))
    这个答案不正确。你忘记了
    模板视图