You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django 1.11部署至PythonAnywhere后静态文件无法加载求助

Fixing Django 1.11 Static Files Not Loading on PythonAnywhere

Hey there, let's work through this static file issue you're hitting—it's a super common pitfall when deploying to PythonAnywhere, but we can get it sorted quickly. Here's a step-by-step breakdown of what to check:

1. Verify Your Static Settings in settings.py

First, double-check your configuration to make sure Django knows where to collect and serve static files:

  • STATIC_ROOT: This must be an absolute path to the directory where collectstatic will gather all static files. For example:
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    
    Important: Don't set this to the same directory as your local static folder (the one in STATICFILES_DIRS)
  • STATIC_URL: Should be set to /static/ (matches what you're using in your templates)
  • STATICFILES_DIRS: Make sure this points to your local development static folder, so collectstatic knows where to pull files from:
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static'),
    ]
    

2. Re-Run collectstatic Correctly

Even if you ran it before, confirm you're executing it in the right context:

  • Log into your PythonAnywhere console, navigate to your project's root directory (where manage.py lives)
  • If you're using a virtual environment, activate it first (e.g., source myenv/bin/activate)
  • Run:
    python manage.py collectstatic
    
    You should see output listing all the files being copied to your STATIC_ROOT directory—if no files are being copied, double-check your STATICFILES_DIRS setting.

3. Configure PythonAnywhere's Static File Mapping

This is the most often missed step! PythonAnywhere doesn't automatically serve static files from your STATIC_ROOT—you have to tell it to:

  1. Go to the Web tab in your PythonAnywhere dashboard
  2. Scroll down to the Static files section
  3. Add a new entry:
    • URL: /static/ (matches your STATIC_URL)
    • Directory: Paste the absolute path to your STATIC_ROOT directory (e.g., /home/yourusername/yourprojectname/staticfiles)
  4. Click Save and then hit the Reload button at the top of the Web tab to apply the changes.

4. Check File Permissions

Occasionally, file permissions can block PythonAnywhere's web server from accessing your static files. In the PythonAnywhere console, run this command to ensure read access:

chmod -R 755 /path/to/your/staticfiles

Replace /path/to/your/staticfiles with your actual STATIC_ROOT path.

5. Ensure Proper Template References

While you said your hardcoded /static/... path works locally, it's better practice to use Django's static template tag to avoid environment-related issues:

  • At the top of your template, add:
    {% load static %}
    
  • Then reference your file like this:
    <script src="{% static 'jquery.templates/jquery.templates.js' %}"></script>
    

This ensures Django uses the correct STATIC_URL regardless of your deployment environment.

Go through these steps in order, and your static files should start loading without a hitch. The PythonAnywhere static mapping is usually the culprit here—don't forget to reload your app after setting it up!

内容的提问来源于stack exchange,提问作者vladimir.gorea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:39:50