简体   繁体   English

在Django中,什么是在任何地方的python上进行媒体部署的正确设置

[英]In django, what is correct setup for media deployment on python anywhere

I am trying to deploy my django app on python anywhere, but I believe media_urls or media_root may be set incorrectly. 我正在尝试在任何地方的python上部署django应用程序,但我相信media_urls或media_root可能设置不正确。 I also spent time trying to figure out how to print all valid urls on django, but nothing work for me on this link. 我还花了一些时间试图弄清楚如何在Django上打印所有有效的url,但是在此链接上对我没有任何帮助。 Django : How can I see a list of urlpatterns? Django:如何查看urlpatterns列表? .

I received a "ModuleNotFoundError: No module named 'django.core.urlresolvers'" 我收到“ ModuleNotFoundError:没有名为'django.core.urlresolvers'的模块”

from .base import *

DEBUG = False

ALLOWED_HOSTS = ["*"]

MEDIA_ROOT = os.path.join(BASE_DIR, "images")
MEDIA_URL = '/snapcapsule/'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")

我在pythonanywhere上设置路径的图片 图片获取图片请求

Answer 回答

回答

Your static file mapping for images is at at /images, but you're accessing the image from /snapcapsule/images. 图像的静态文件映射位于/ images,但是您正在从/ snapcapsule / images访问图像。 Either change your media_url or your static file mapping so that they match. 更改您的media_url或您的静态文件映射,以使它们匹配。

Most of the time, I server static and media with django at local and with nginx in server. 大多数情况下,我使用本地的django和服务器中的nginx来处理静态和媒体。

At local: 在当地:

# at the end of urls.py
if settings.DEBUG:
    # debug toolbar
    import debug_toolbar

    urlpatterns.insert(0, path("__debug__/", include(debug_toolbar.urls)))

    # static and media
    from django.conf.urls.static import static
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns

    urlpatterns.extend(
        staticfiles_urlpatterns()
        + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    )

In server: 在服务器中:

# nginx.conf
server {
    ...
    location /static/ {
        alias /path/to/project/static_collection/;
    }

    location /media/ {
        alias /path/to/project/media/;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM