python-3.x - 系统找不到指定的路径: error returned when changing field in admin page Django

标签 python-3.x django django-models error-handling

大家好,我只是按照本教程(https://realpython.com/get-started-with-django-1/)为我的Django投资组合站点项目集成了一个项目应用程序。
设置一切正常,直到我尝试登录Django的管理页面以向数据库中的Project模型添加更多对象。
单击添加后,它返回一个错误页面,并显示如下:
错误返回页[1]:/image/co1Cy.png
追溯

Traceback (most recent call last):
  File "C:\Users\username\anaconda3\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\username\anaconda3\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\username\anaconda3\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\username\anaconda3\lib\site-packages\django\contrib\admin\options.py", line 607, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "C:\Users\username\anaconda3\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\username\anaconda3\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\username\anaconda3\lib\site-packages\django\contrib\admin\sites.py", line 231, in inner
    return view(request, *args, **kwargs)
  File "C:\Users\username\anaconda3\lib\site-packages\django\contrib\admin\options.py", line 1638, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "C:\Users\username\anaconda3\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\username\anaconda3\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\username\anaconda3\lib\site-packages\django\contrib\admin\options.py", line 1522, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "C:\Users\username\anaconda3\lib\site-packages\django\contrib\admin\options.py", line 1555, in _changeform_view
    ModelForm = self.get_form(request, obj, change=not add)
  File "C:\Users\username\anaconda3\lib\site-packages\django\contrib\admin\options.py", line 669, in get_form
    fields = flatten_fieldsets(self.get_fieldsets(request, obj))
  File "C:\Users\username\anaconda3\lib\site-packages\django\contrib\admin\options.py", line 328, in get_fieldsets
    return [(None, {'fields': self.get_fields(request, obj)})]
  File "C:\Users\username\anaconda3\lib\site-packages\django\contrib\admin\options.py", line 319, in get_fields
    form = self._get_form_for_get_fields(request, obj)
  File "C:\Users\username\anaconda3\lib\site-packages\django\contrib\admin\options.py", line 659, in _get_form_for_get_fields
    return self.get_form(request, obj, fields=None)
  File "C:\Users\username\anaconda3\lib\site-packages\django\contrib\admin\options.py", line 702, in get_form
    return modelform_factory(self.model, **defaults)
  File "C:\Users\username\anaconda3\lib\site-packages\django\forms\models.py", line 554, in modelform_factory
    return type(form)(class_name, (form,), form_class_attrs)
  File "C:\Users\username\anaconda3\lib\site-packages\django\forms\models.py", line 257, in __new__
    apply_limit_choices_to=False,
  File "C:\Users\username\anaconda3\lib\site-packages\django\forms\models.py", line 178, in fields_for_model
    formfield = formfield_callback(f, **kwargs)
  File "C:\Users\username\anaconda3\lib\site-packages\django\contrib\admin\options.py", line 186, in formfield_for_dbfield
    return db_field.formfield(**kwargs)
  File "C:\Users\username\anaconda3\lib\site-packages\django\db\models\fields\__init__.py", line 1666, in formfield
    **kwargs,
  File "C:\Users\username\anaconda3\lib\site-packages\django\db\models\fields\__init__.py", line 927, in formfield
    return form_class(**defaults)
  File "C:\Users\username\anaconda3\lib\site-packages\django\forms\fields.py", line 1110, in __init__
    for f in os.scandir(self.path):

Exception Type: FileNotFoundError at /admin/projects/project/add/
Exception Value: [WinError 3] The system cannot find the path specified: '/projects/img/'

我的models.py
from django.db import models 
class Project(models.Model):
  title = models.CharField(max_length=100)
  description = models.TextField()
  technology = models.CharField(max_length=20)
  image = models.FilePathField(path='/projects/img/')
我的文件夹看起来像这样
 django-project
    projects
      static
        projects
          img
             -project1.png
             -project2.png
             -project3.png
...

最佳答案

感谢您的帮助,我发现了这个错误,
FilePathField方法接受比我认为需要的更多显式路径,因此我用将此模型替换为,它现在就像一个魅力

from django.db import models

# Create your models here.
class Project(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField()
    technology = models.CharField(max_length=20)
    image = models.FilePathField(path='projects/static/projects/img/')
    def __str__(self):
        return self.title```

关于python-3.x - 系统找不到指定的路径: error returned when changing field in admin page Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62503355/

相关文章:

Python 无法在非持久性属性上设置属性

django - facebook django 应用程序的 oauth 中的 redirect_uri 错误

python - Django:对模型使用两个不同的数据库,具有两种不同的结构

django - 在 Crispy Forms 表单集中使用 HTML

python - 在 Django 中使用空值创建左连接

javascript - 我如何解析这些非 JSON 数据?

python-3.x - 在 Pandas DataFrame 上应用 TimeZoneFinder 函数

python - Django1.4 : How to use order_by in template?

django - Django 中使用的 PostgreSQL View (managed=False)不能正确同步

python - 如何在 Django 管理自定义列中获取请求参数?