React-django connectivity issues, 500 internal server errors
I’m working on a project using react as the frontend and Django rest api as backend. Unfortunately, I’m having some issues. Here are the issues I’m facing:
From the Django setting.py file, I set the static directory folder to “build” (for css, images and JavaScript) and media folder (for user uploaded files) file to “media” as shown below. But I observed that only the “media” folder shows up in the directory structure and not the “build” folder. PLEASE WHAT COULD BE PREVENTING THE “BUILD” FOLDER FROM SHOWING?
STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'build/static') ]
STATIC_ROOT = os.path.join(BASE_DIR, 'static') MEDIA_URL = '/medial/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
I understand that normally when you run “npm build” and copy the build folder generated from the frontend to replace the one in the Django backend, then you can begin to use “localhost:8000” to access your pages rather than continue to use “localhost:3000”. I did the above, but I wasn’t able to load the pages using “localhost:8000” but instead getting 404 error. PLEASE WHAT COULD BE THE ISSUES?
I created some routes (post and get), tested all the routes in postman as well as the browser, and they (routes) are all working perfectly. However, when I run them from the react frontend, they aren’t working. By that I mean the data are not submitted to the database (for the post routes) as well not being displayed on the web page (for the get routes). Strangely though, one of the post routes is working (that is, submitting data to the database) from the react frontend. My concern is why the rest routes (post and get) are not working and there is no error message except the vague “500 internal server error” from the console. I use about the same code logic for virtually all the routes in the “views.py” file as well as from the react frontend files. Here is the views.py file for the only route that is working (i.e., from the react frontend):
- class CreateContactView(APIView):
-
permission_classes = (permissions.AllowAny, )
-
def post(self, request, format=None):
-
data = self.request.data
-
try:
-
contact = Contact(
-
name=data['name'], email=data['email'], phone=data['phone'], subject=data['subject'], message=data['message'])
-
contact.save()
-
return Response({'success': 'Message sent successfully'})
-
except:
-
return Response({'error': 'Message failed to send'})
and here is one of the others not working. Yet they are of the same code logic
class PartnerCreate(APIView):
permission_classes = (permissions.AllowAny, )
def post(self, request, format=None):
data = self.request.data
try:
partners = Partners(
name=data['name'], email=data['email'], phone=data['phone'],
gendar=data['gendar'], area_spec=data['area_spec'], year_exp=['year_exp'], passport=['passport'], profile=['profile'])
partners.save()
return Response({'success': 'Registration successful'})
except:
return Response({'error': 'Something went wrong'})
I HAVE RAISED 4 ISSUES, PLEASE CAN ANYONE HELP ME FIGURE OUT WHAT’S WRONG? Thanks everyone.