用户无法上传到Pythonanywhere上的Media目录

时间:2017-02-28 12:00:22

标签: python django pythonanywhere

我想提交一个有文件字段的表单,在开发模式下我可以成功提交它但是当涉及到pythonanywhere我被重定向到我的自定义失败页面,这意味着表单没有&# 39; t通过。

当没有附件时保存表单,它适用于debug true&在developpement(username.pythonanywhere.com)上为false,但在产品上没有(www.domain.com)

以下是我配置pythonanywhere文件的方法:

settings.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = False

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

urls.py

urlpatterns = [
    ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

models.py

class AskTicket(models.Model):
    document = models.FileField(upload_to='attachments')
    ...

以下是我设置静态文件管理器的方法:

enter image description here

为什么用户无法下载文件?我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

如何关注DjangoGirls Tutorial

您可以在https://tutorial.djangogirls.org/en/deploy/

找到它

所以你必须这样做:

import os
import sys

path = '/home/<your-PythonAnywhere-username>/my-first-blog'  # use your own PythonAnywhere username here

if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler
application = StaticFilesHandler(get_wsgi_application())
相关问题