能否在PythonAnywhere.com部署React前端与Django后端API应用?
Can I deploy a React frontend + Django backend on PythonAnywhere?
Absolutely! PythonAnywhere fully supports running a Django API backend alongside a React frontend—this is a super common setup, and I’ve walked through it with plenty of developers. Let’s break down exactly how to make it work smoothly.
1. First: Get Your Django Backend Up and Running
Start with your Django API just like you would any standard Django project on PythonAnywhere:
- Upload your code via Git, or drag-and-drop files using the PythonAnywhere file manager.
- Set up a virtual environment (don’t skip this!)—run
mkvirtualenv --python=3.11 my-django-env(use your preferred Python version) then install dependencies withpip install -r requirements.txt. - Tweak your Django settings:
- Add your PythonAnywhere subdomain (e.g.,
yourusername.pythonanywhere.com) toALLOWED_HOSTS. - Flip
DEBUGtoFalsefor production (critical for security). - Install
django-cors-headersand configureCORS_ALLOWED_ORIGINSto include your React frontend’s URL (we’ll cover that URL in the next section).
- Add your PythonAnywhere subdomain (e.g.,
- Use PythonAnywhere’s WSGI config wizard to point to your project’s
wsgi.pyfile—if you started a new Django project from their dashboard, this is already set up, but you can edit it manually too. - Run migrations with
python manage.py migrateand collect static files withpython manage.py collectstatic(make sureSTATIC_ROOTis defined in your settings).
2. Deploying Your React Frontend
React apps are static once built, so you’ve got two solid options here:
Option 1: Serve React from Django’s Static Files
This is great if you want everything under one domain:
- Build your React app locally with
npm run build—this creates abuildfolder with all minified assets. - Copy the entire contents of
buildinto a subfolder of your Django project’s static directory, likestatic/react-build. - Set up a Django view to serve the React
index.htmlfile. You can use a simpleTemplateViewin yoururls.py:from django.views.generic import TemplateView urlpatterns = [ # ... your API endpoints here ... path('', TemplateView.as_view(template_name='react-build/index.html')), ] - Note: If you’re using React Router, switch to
HashRouterinstead ofBrowserRouter—PythonAnywhere’s static file serving doesn’t handle fallback routes for SPAs by default.
Option 2: Host React as a Separate Static Site
If you prefer keeping frontend and backend separate:
- Build your React app locally with
npm run build, then upload thebuildfolder to PythonAnywhere (I usually put it in/home/yourusername/react-build). - Go to the "Web" tab in PythonAnywhere, click "Add a new web app", select "Static files", then set:
- URL:
/(if you want React to be the main site) or/frontend/ - Source directory:
/home/yourusername/react-build
- URL:
- Update your React app’s API calls to point to your Django backend URL (e.g.,
https://yourusername.pythonanywhere.com/api/).
3. Critical Configuration Tips
- CORS is non-negotiable: Make sure
django-cors-headersis installed, added toINSTALLED_APPSandMIDDLEWARE, andCORS_ALLOWED_ORIGINSincludes your React site’s full URL (e.g.,["https://yourusername.pythonanywhere.com"]). - Prefix your API routes: Keep all Django endpoints under
/api/so there’s no conflict with React routes or static files. - Secure your secrets: Use PythonAnywhere’s "Secret keys" feature or a
.envfile (never commit this to Git) to store database credentials, API keys, etc. For Django, usepython-dotenvto load these variables into your settings.
4. Troubleshooting Common Hiccups
- React can’t reach the API? Double-check CORS settings, verify the backend URL is correct, and check Django’s error logs in PythonAnywhere (under the "Web" tab) for any server errors.
- Static files not loading? For Django, confirm
collectstaticran successfully and that your static URL is configured right. For React static sites, make sure the source directory in PythonAnywhere points directly to thebuildfolder. - React Router 404s? If you insist on
BrowserRouter, you’ll need to set up a catch-all route in Django that serves the Reactindex.htmlfor any non-API path. Add this to your Djangourls.py:path('', TemplateView.as_view(template_name='react-build/index.html')), path('<path:path>', TemplateView.as_view(template_name='react-build/index.html')),
内容的提问来源于stack exchange,提问作者Mohd Ahshan Danish
相关产品推荐
相关产品推荐

