【发布时间】:2021-02-28 07:46:24
【问题描述】:
我正在尝试使用 django rest 框架在我的 api 中使用图像,但我仍然收到错误 404 Page not found。这是我的代码 -
Models.py
class SampleModel(models.Model):
text = models.CharField(max_length=100, null=True, blank=True)
image = models.ImageField(upload_to='images/', null=True, blank=True)
serializers.py
class SampleSerializer(serializers.ModelSerializer):
image = serializers.ImageField(max_length=None, use_url=True)
class Meta:
model = SampleModel
fields = ['text', 'image']
views.py
class SampleView(generics.ListAPIView, generics.CreateAPIView):
queryset = SampleModel.objects.all()
serializer_class = SampleSerializer
urls.py
urlpatterns = [path('sample/', SampleView.as_view(), name='sample')]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, "assets")
MEDIA_URL = '/assets/'
我尝试在谷歌上搜索解决方案并尝试了一些东西,但我仍然遇到同样的错误。 我尝试创建另一个项目并使用与上面相同的代码,但它仍然给我同样的错误。
【问题讨论】:
标签: python django api django-models django-rest-framework