0

So my django/react project works find on my local machine but when I deploy it to pythonanywhere.com I get this message below when I try to go to any of the routes ("/", "/factors", "/chart"). I have seen some other posts on how to tell django where to go to if the url corresponds to the frontend React routes (since that is the only place the routes are defined) but I'm unsure what to put in the views. Is the only way to get this to work to load a generic html template with a django view and then call the React routes from there? Why does it work in development without doing this but the same url paths don't display anything on the live site? The /api route correctly displays the django api interface so at least I know some routes are working.

Page not found (404) Request Method: GET Request URL: https://elledee.pythonanywhere.com/ Using the URLconf defined in factorsViewer.urls, Django tried these URL patterns, in this order:

admin/ api/ The empty path didn’t match any of these.

Here is my urls.py project file (I don't have a urls.py in the fv app).

"""time_factor URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
admin.autodiscover()
from django.urls import path, include
from rest_framework import routers
from fv import views
from django.urls import re_path

router = routers.DefaultRouter(trailing_slash=False)
router.register(r'factor', views.FactorViewSet)
router.register(r'fund/get_queryset', views.FundViewSet, basename='Fund')
router.register(r'price/get_queryset', views.PriceViewSet, basename='Price')
# router.register(r'master', views.MasterViewSet)


# This wrapper turns your list into a regex URL matcher
react_views_regex = r'\/|\b'.join([

    # List all your react routes here
    '/',
    '/factors',
    '/chart',

]) + r'\/'

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include(router.urls)),
    # Change views.index to whatever your view is
    re_path(react_views_regex, views.index),
    # match the root
    # url(r'^$', base_view),
    # match all other pages
    # re_path(r'^(?:.*)/?$', base_view),
    # re_path('(^(?!(api|admin)).*$)',
    #     TemplateView.as_view(template_name="build/index.html")),
]

# try this
# https://stackoverflow.com/questions/40826295/react-routing-and-django-url-conflict
Eric Egana
  • 11
  • 1
  • 2
  • What errors do you see in your logs? (link to logs are available on the "Web" page on PythonAnywhere). – Filip May 17 '21 at 10:24
  • Thanks for replying Filip. I think the reason it was working on my local machine is because I was viewing it at localhost:3000 which is what the React server was running, so it was aware of the frontend routes. For the backend routes to work, I need to have an html template load in views.index and then in that template load App.js. let me try that and see if this gets resolved. – Eric Egana May 17 '21 at 14:08

0 Answers0