gpt4 book ai didi

django - 为什么图像没有由 django 提供服务?

转载 作者:行者123 更新时间:2023-12-04 07:18:30 24 4
gpt4 key购买 nike

我正在尝试显示 django 数据库中的图像。在目录的数据库图像链接中也给出了正确的。但是当我打开那个链接时,它给出了一个错误“TemplateDoesNotExist at/media/categories/abc.png”
在该链接中甚至可以使用图像。
模型.py

from django.db import models
def upload_path(instance,filename):
return 'categories/{filename}'.format(filename=filename)

class Categories(models.Model):
name = models.CharField(max_length=255, unique=True)
description = models.CharField(max_length=500)
image_url = models.ImageField(blank=True, null = True, upload_to= upload_path)
REQUIRED_FIELDS = ['name','description','image_url']
设置.py
    STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'
CORS_ALLOW_ALL_ORIGINS = True
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'build/static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
urls.py

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

urlpatterns = [
path('auth/', include('djoser.urls')),
path('admin/', admin.site.urls),
path('auth/', include('djoser.urls.jwt')),
path('auth/', include('djoser.social.urls')),
path('adminuser/', include('adminuser.urls')),
]

urlpatterns += [re_path(r'^.*', TemplateView.as_view(template_name='index.html'))]
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
Views.py
class CategoriesListCreateAPI(GenericAPIView, ListModelMixin , CreateModelMixin):
queryset = Categories.objects.all()
serializer_class = CategoriesSerializer
permission_classes = (AllowAny,)

def get(self, request , *args, **kwargs):
return self.list(request, *args, **kwargs)

def post(self, request , *args, **kwargs):
return self.create(request, *args, **kwargs)

# Retrieve, update and delete (pk required)
class RUDCategoriesAPI(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin):
queryset = Categories.objects.all()
serializer_class = CategoriesSerializer
permission_classes = (AllowAny,)

def get(self, request , *args, **kwargs):
return self.retrieve(request, *args, **kwargs)

def put(self, request , *args, **kwargs):
return self.update(request, *args, **kwargs)

def put(self, request , *args, **kwargs):
return self.partial_update(request, *args, **kwargs)

def delete(self, request , *args, **kwargs):
return self.destroy(request, *args, **kwargs)
在这里请看我的 API API当我点击那个链接“http://localhost:8000/media/categories/feed.png”上传图片后
它给了我一个错误页面。
Error

最佳答案

你的网址有

urlpatterns += [re_path(r'^.*', TemplateView.as_view(template_name='index.html'))]
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
第一 re_path捕获每个请求,因此永远不会到达静态 URL。翻转这些:
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT) 
urlpatterns += [re_path(r'^.*', TemplateView.as_view(template_name='index.html'))]

关于django - 为什么图像没有由 django 提供服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68648456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com