0

在我的 settings.py 中,我添加了以下内容

print(BASE_DIR)
print(MEDIA_URL)
print(MEDIA_ROOT)

它打印以下内容

d:/OneDrive/!code_projects/!django_projects/mgTut
/media/
d:/OneDrive/!code_projects/!django_projects/mgTut/media/

但是当我启动服务器时,我看到了这个错误

FileNotFoundError: [Errno 2] No such file or directory: 'd:\\OneDrive\\!code_projects\\!django_projects\\mgTut\\media\\media\\images\\profile_image\\0C0A8604_1_mKlAyCG.JPG'

我不太确定从这里去哪里。我的 media_root 应该被替换,但它似乎没有被调用。

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

我的models.py类:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    description = models.CharField(max_length=200, default='')
    city = models.CharField(max_length=50, default='')
    state = models.CharField(max_length=2, default='')
    website = models.URLField(default='')
    phone = models.IntegerField(default=0)
    avatar = models.ImageField(upload_to=b'media/images/profile_image',
                               default='media/images/profile_image/Profile_Blank.PNG')
    avatar_thumbnail = ImageSpecField(source='avatar',
                                      processors=[ResizeToFill(100, 100)],
                                      format='JPEG',
                                      options={'quality': 60})

    def __str__(self):
        return '%s - %s' % (self.user, self.description)


profile = UserProfile.objects.all()[0]
print(profile.avatar_thumbnail.url)    # > /media/CACHE/images/982d5af84cddddfd0fbf70892b4431e4.jpg
print(profile.avatar_thumbnail.width)  # > 100

完整错误:

d:/OneDrive/!code_projects/!django_projects/mgTut
/media/
d:/OneDrive/!code_projects/!django_projects/mgTut/media/
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x000002EDDFA09048>
Traceback (most recent call last):
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
    autoreload.raise_last_exception()
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\django\utils\autoreload.py", line 250, in raise_last_exception
    six.reraise(*_exception)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models()
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\django\apps\config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Program Files\Python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "d:\OneDrive\!code_projects\!django_projects\mgTut\account\models.py", line 27, in <module>
    print(profile.avatar_thumbnail.url)    # > /media/CACHE/images/982d5af84cddddfd0fbf70892b4431e4.jpg
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\imagekit\cachefiles\__init__.py", line 84, in url
    return self._storage_attr('url')
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\imagekit\cachefiles\__init__.py", line 74, in _storage_attr
    existence_required.send(sender=self, file=self)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\django\dispatch\dispatcher.py", line 193, in send
    for receiver in self._live_receivers(sender)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\django\dispatch\dispatcher.py", line 193, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\imagekit\registry.py", line 53, in existence_required_receiver
    self._receive(file, 'on_existence_required')
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\imagekit\registry.py", line 61, in _receive
    call_strategy_method(file, callback)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\imagekit\utils.py", line 166, in call_strategy_method
    fn(file)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\imagekit\cachefiles\strategies.py", line 15, in on_existence_required
    file.generate()
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\imagekit\cachefiles\__init__.py", line 93, in generate
    self.cachefile_backend.generate(self, force)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\imagekit\cachefiles\backends.py", line 109, in generate
    self.generate_now(file, force=force)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\imagekit\cachefiles\backends.py", line 96, in generate_now
    file._generate()
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\imagekit\cachefiles\__init__.py", line 97, in _generate
    content = generate(self.generator)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\imagekit\utils.py", line 152, in generate
    content = generator.generate()
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\imagekit\specs\__init__.py", line 150, in generate
    img = open_image(self.source)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\pilkit\utils.py", line 21, in open_image
    target.seek(0)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\django\core\files\utils.py", line 20, in <lambda>
    seek = property(lambda self: self.file.seek)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\django\db\models\fields\files.py", line 51, in _get_file
    self._file = self.storage.open(self.name, 'rb')
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\django\core\files\storage.py", line 38, in open
    return self._open(name, mode)
  File "D:\OneDrive\!code_projects\!django_projects\windows_venv\lib\site-packages\django\core\files\storage.py", line 300, in _open
    return File(open(self.path(name), mode))
FileNotFoundError: [Errno 2] No such file or directory: 'd:\\OneDrive\\!code_projects\\!django_projects\\mgTut\\media\\media\\images\\profile_image\\0C0A8604_1_mKlAyCG.JPG'

更新:它与 ImageKit 提供的 URL 隔离。打印profile.avatar_thumbnail.url是引发错误的原因。

尝试添加替换,同样的问题。

print(profile.avatar_thumbnail.url.replace('\\', '/'))    # > /media/CACHE/images/982d5af84cddddfd0fbf70892b4431e4.jpg
print(profile.avatar_thumbnail.width.replace('\\', '/'))  # > 100

网址会议:

from django.conf.urls import url, include
from django.contrib import admin
from django.urls import reverse_lazy
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.auth.views import (
    LoginView,
    LogoutView,
    PasswordResetView,
    PasswordResetDoneView,
    PasswordChangeView,
    PasswordChangeDoneView,
    PasswordResetView,
    PasswordResetConfirmView,
    PasswordResetCompleteView
)
from . import views

urlpatterns = [
    url(r'^login/$',
        LoginView.as_view(template_name='account/login.html'),
        name='login'),
    url(r'^logout/$',
        LogoutView.as_view(next_page=reverse_lazy('login')),
        name='logout'),
    url(r'^register/$',
        views.register,
        name='register'),
    url(r'^password_change/$',
        PasswordChangeView.as_view(
            template_name='account/password_change_form.html'
        ),
        name='password_change'),
    url(r'^password_change/done/$',
        PasswordChangeDoneView.as_view(
            template_name='account/password_change_done.html'
            ),
        name='password_change_done'),
    url(r'^password_reset/$',
        PasswordResetView.as_view(
            template_name='account/password_reset_form.html'
        ),
        name='password_reset'),
    url(r'^password_reset/done/$',
        PasswordResetDoneView.as_view(
            template_name='account/password_reset_done.html'
        ),
        name='password_reset_done'),
    url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
        PasswordResetConfirmView.as_view(
            template_name='account/password_reset_confirm.html'
        ),
        name='password_reset_confirm'),
    url(r'^reset/done/$',
        PasswordResetCompleteView.as_view(
            template_name='account/password_reset_complete.html'
            ),
        name='password_reset_complete'),
    url(r'^admin/',
        admin.site.urls),
    url(r'^$',
        views.home,
        name='homepage'),
    url(r'^account/',
        include('account.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
4

0 回答 0