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:

  1. 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')

  2. 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?

  3. 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):

    1. class CreateContactView(APIView):
    2.  permission_classes = (permissions.AllowAny, )
      
    3.  def post(self, request, format=None):
      
    4.      data = self.request.data
      
    5.     try:
      
    6.         contact = Contact(
      
    7.             name=data['name'], email=data['email'], phone=data['phone'], subject=data['subject'], message=data['message'])
      
    8.         contact.save()
      
    9.         return Response({'success': 'Message sent successfully'})
      
    10.     except:
      
    11.         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.

Back to Top