Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将图像保存到用户模型中_Python_Django_Imagefield - Fatal编程技术网

Python 将图像保存到用户模型中

Python 将图像保存到用户模型中,python,django,imagefield,Python,Django,Imagefield,我正在尝试在我的用户模型中创建一个“化身”字段(尝试使用所有的库,但不太喜欢它们) 在setting.py中 MEDIA_ROOT = '/media/' 另外,我甚至没有使用表单,而是直接从管理员上传,问题是图像没有保存到templates/media/photos文件夹中 此外,我没有任何相关视图,只是尝试将图像保存到文件夹中,以便更好地了解文件系统的工作原理 编辑:拼写错误我不知道你的媒体URL是什么样子,但媒体设置应该是这样的: BASE_DIR = os.path.dirname(o

我正在尝试在我的用户模型中创建一个“化身”字段(尝试使用所有的库,但不太喜欢它们)

在setting.py中

MEDIA_ROOT = '/media/'
另外,我甚至没有使用表单,而是直接从管理员上传,问题是图像没有保存到templates/media/photos文件夹中

此外,我没有任何相关视图,只是尝试将图像保存到文件夹中,以便更好地了解文件系统的工作原理


编辑:拼写错误

我不知道你的媒体URL是什么样子,但媒体设置应该是这样的:

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

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
不,您不能将媒体文件保存在
templates
文件夹下。这没有任何意义<代码>模板应该有html文件,
静态
应该有静态文件(css、js、静态图像)和
媒体
用户上传的文件

这样,如果有人加入你的项目或者你的项目变得更大,你就不会陷入混乱/误导性的文件夹名称中,否则会很痛苦,相信我

对于404问题

您需要更改根目录中的主URL.py以包含以下内容:

if settings.DEBUG:
    urlpatterns += patterns('',
       url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
       {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
    url(r'', include('django.contrib.staticfiles.urls')),
)
if settings.DEBUG:
urlpatterns+=模式(“”,
url(r'^media/(?P.*)$,'django.views.static.service',
{'document_root':settings.MEDIA_root,'show_index':True}),
url(r“”,包括('django.contrib.staticfiles.url'),
)

使用本地环境时,您需要配置您的设置以正确地为静态介质服务

将其添加到您的设置中。py

DEBUG = True

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

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

MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
from django.conf.urls.static import static


if settings.DEBUG:
    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
将其添加到您的根URL.py

DEBUG = True

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

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

MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
from django.conf.urls.static import static


if settings.DEBUG:
    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

实际上我拼错了,谢谢你指出这一点。当我试图转到图像的URL时,它会在@NicolásCurti处抛出一个404,请在上面的答案中查看我的更新我从何处导入“模式”?“NameError:name'static'未定义”我只是添加了
设置.py
配置来解决该错误。它没有,一定缺少一些非常明显的东西,但是我似乎不明白:(哦,我的错误,从django.conf.urls.static添加
导入static
urls.py
(我将编辑我发布的设置以反映这一点)它现在编译了,但仍然没有将图像保存到文件夹中,因此url仍然抛出404