I want to display an image which I got the src from in the database linked to django on localhost 8000, in react (localhost 3000).
here is my view:
class NextQuestion(APIView):
permission_classes = [IsAuthenticated]
lookup_url_kwarg = 'old_questions'
def post(self, request, format=None):
old_questions = request.data.get(self.lookup_url_kwarg)
if len(old_questions):
old_questions_q = [Q(id=x) for x in old_questions]
questions_valides = Question.objects.exclude(reduce(operator.or_, old_questions_q))
if len(questions_valides) == 0 :
return Response("Il n'y a plus de questions disponibles!")
else:
questions_valides = Question.objects.all()
index = randint(0, len(questions_valides) -1)
new_question = QuestionSerializer(questions_valides[index]).data
return Response(new_question)
My serializer:
class QuestionSerializer(serializers.ModelSerializer):
class Meta:
model = Question
fields = '__all__'
My urls:
from django.contrib import admin
from django.urls import path, include, re_path
from django.views.generic import TemplateView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
######
path('quiz/', include('quiz.urls')),
]
urlpatterns += [re_path(r'^.*', TemplateView.as_view(template_name='index.html'))]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And in my settings:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'build/static')
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
In my .js:
<Fade in={!toggle} {...(!toggle ? { timeout: 2000 } : {})}>
<Paper elevation={0} className={classes.paper}>
<img className={classes.image} src={image} alt="La réponse." />
</Paper>
</Fade >
My app component:
const App = () => (
<Provider store={store}>
<SnackbarProvider maxSnack={5} >
<Router>
<Switch>
<NavLayout>
<Route exact path='/' component={Home} />
<Route exact path='/login' component={Login} />
<Route exact path='/signup' component={Signup} />
<Route exact path='/reset-password' component={ResetPassword} />
<Route exact path='/password/reset/confirm/:uid/:token' component={ResetPasswordConfirm} />
<Route exact path='/activate/:uid/:token' component={Activate} />
</NavLayout>
</Switch>
</Router>
</SnackbarProvider>
</Provider>
);
export default App;
The request return status 200, but the response is empty:
GET
scheme
http
host
127.0.0.1:8000
filename
/media/images/quiz/cochon.png
Adresse
127.0.0.1:8000
État200
OK
VersionHTTP/1.1
Transfert2,42 Ko (taille 2,17 Ko)
Politique de référentsame-origin
Content-Length
2227
Content-Type
text/html; charset=utf-8
Date
Sun, 18 Apr 2021 08:18:28 GMT
Referrer-Policy
same-origin
Server
WSGIServer/0.2 CPython/3.9.2
Vary
Origin
X-Content-Type-Options
nosniff
X-Frame-Options
DENY
Accept
image/webp,*/*
Accept-Encoding
gzip, deflate
Accept-Language
fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Cache-Control
max-age=0
Connection
keep-alive
Cookie
csrftoken=8e5HTXPQRNuesBbAC389HePJYYebgcKNS7Km60kP6uqURbIpNyA3khuuJPQK4V7M; djdt=hide; sessionid=ffneb7uu8ia5ly8f7w45a2ynbemz6886; PGADMIN_INT_KEY=7bde6f56-0a71-41e0-9953-f719d7135568; PGADMIN_LANGUAGE=en; tabstyle=html-tab
Host
127.0.0.1:8000
Referer
http://127.0.0.1:8000/
User-Agent
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0
My variable image has this value: /media/images/quiz/animal-2029280_1280.png
It should match the path corresponding to the image, yet it doesn't load. Did I forget a manipulation? Or a parameter error? I only have a problem with the image, the rest is fine.
I have the same result if i run my app on localhost 8000 or 3000.
Thx!