从 2 个模型创建表单 python django: [Errno 2] 没有那个文件或目录
create form from 2 models python django: [Errno 2] No such file or directory
我正在尝试使用 python 和 django 从 2 个模型创建一个表单。
下面显示了我的模型:
# Create your models here.
class Action(models.Model):
name = models.CharField("Action name", max_length=50)
keywords = models.CharField("Keywords", max_length=50)
object = models.CharField("Object", max_length=50, blank=True, null=True)
uploadDate = models.DateField("Date", default=get_current_date)
UploadedBy = models.CharField("UploadedBy", max_length=50, default="")
class Image(models.Model):
image = models.FileField(upload_to=get_upload_file_name, default="")
action = models.ForeignKey(Action)
和表格 类:
class ActionForm(ModelForm):
#bind form to Action model
class Meta:
model = Action
fields = ['name','keywords', 'object', 'UploadedBy', 'uploadDate']
class ImageForm(ModelForm):
class Meta:
model= Image
fields =['image']
从 createForm.html 模板创建页面的视图中的代码:
def actioncreate(request):
if request.method == "GET":
#create the object - Actionform
form = ActionForm;
form2 = ImageForm;
#pass into it
return render(request,'app/createForm.html', { 'form':form, 'form2':form2})
elif request.method == "POST":
# take all of the user data entered to create a new action instance in the table
form = ActionForm(request.POST, request.FILES)
form2 = ImageForm(request.POST, request.FILES)
if form.is_valid() and form2.is_valid():
#prepare action model
act = form.save(commit=False)
#set the action_id Foreignkey
act.action_id = form2.save()
act.save()
return HttpResponseRedirect('/actions')
else:
form = ActionForm()
form2 = ImageForm;
return render(request,'app/createForm.html', { 'form':form, 'form2':form2 })
表单模板:
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<ul>
{{ form.as_ul }}
{{ form2.as_ul }}
</ul>
<button type="submit">Save</button>
<button name="cancel" onclick="navigate({% url 'home' %})">Cancel</button>
</form>
访问页面时表单显示正常,但是提交后出现此错误:
[Errno 2] 没有那个文件或目录
它似乎不喜欢这条线:
act.action_id = form2.save()
最确定创建的模型具有我所指的所有字段,尽管我收到此错误,但前半部分 (ActionForm) 已保存到数据库中,
但不是 imageForm。一些帮助将不胜感激!
编辑
错误:
OSError at /actions/create
[Errno 2] No such file or directory
Request Method: POST
Request URL: http://127.0.0.1:8000/actions/create
Django Version: 1.8.7
Exception Type: OSError
Exception Value:
[Errno 2] No such file or directory
Exception Location: C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\core\files\storage.py in _save, line 248
Python Executable: C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\Scripts\python.exe
Python Version: 2.7.0
Python Path:
['C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2',
'C:\WINDOWS\SYSTEM32\python27.zip',
'C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\DLLs',
'C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib',
'C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\plat-win',
'C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\lib-tk',
'C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\Scripts',
'C:\Python27\Lib',
'C:\Python27\DLLs',
'C:\Python27\Lib\lib-tk',
'C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env',
'C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages']
Server time: Thu, 28 Jan 2016 15:24:04 +0000
追溯
Request Method: POST
Request URL: http://127.0.0.1:8000/actions/create
Django Version: 1.8.7
Python Version: 2.7.0
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
'django.contrib.admin',
'django.contrib.admindocs')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\app\views.py" in actioncreate
53. act.action_id = form2.save()
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\forms\models.py" in save
459. construct=False)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\forms\models.py" in save_instance
105. instance.save()
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\base.py" in save
734. force_update=force_update, update_fields=update_fields)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\base.py" in save_base
762. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\base.py" in _save_table
846. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\base.py" in _do_insert
885. using=using, raw=raw)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\manager.py" in manager_method
127. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\query.py" in _insert
920. return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
973. for sql, params in self.as_sql():
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
931. for obj in self.query.objs
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\fields\files.py" in pre_save
314. file.save(file.name, file, save=False)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\fields\files.py" in save
93. self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\core\files\storage.py" in save
63. name = self._save(name, content)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\core\files\storage.py" in _save
248. fd = os.open(full_path, flags, 0o666)
Exception Type: OSError at /actions/create
Exception Value: [Errno 2] No such file or directory
您需要指定 MEDIA_ROOT 的子目录以上传至您本地文件系统中存在的目录。检查 get_upload_file_name 函数返回的内容并检查它是否是本地文件系统上 MEDIA_ROOT(在 settings.py 中定义)的子目录:
class Image(models.Model):
image = models.FileField(upload_to=get_upload_file_name, default="")
看这里:
https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.FileField.upload_to
我正在尝试使用 python 和 django 从 2 个模型创建一个表单。 下面显示了我的模型:
# Create your models here.
class Action(models.Model):
name = models.CharField("Action name", max_length=50)
keywords = models.CharField("Keywords", max_length=50)
object = models.CharField("Object", max_length=50, blank=True, null=True)
uploadDate = models.DateField("Date", default=get_current_date)
UploadedBy = models.CharField("UploadedBy", max_length=50, default="")
class Image(models.Model):
image = models.FileField(upload_to=get_upload_file_name, default="")
action = models.ForeignKey(Action)
和表格 类:
class ActionForm(ModelForm):
#bind form to Action model
class Meta:
model = Action
fields = ['name','keywords', 'object', 'UploadedBy', 'uploadDate']
class ImageForm(ModelForm):
class Meta:
model= Image
fields =['image']
从 createForm.html 模板创建页面的视图中的代码:
def actioncreate(request):
if request.method == "GET":
#create the object - Actionform
form = ActionForm;
form2 = ImageForm;
#pass into it
return render(request,'app/createForm.html', { 'form':form, 'form2':form2})
elif request.method == "POST":
# take all of the user data entered to create a new action instance in the table
form = ActionForm(request.POST, request.FILES)
form2 = ImageForm(request.POST, request.FILES)
if form.is_valid() and form2.is_valid():
#prepare action model
act = form.save(commit=False)
#set the action_id Foreignkey
act.action_id = form2.save()
act.save()
return HttpResponseRedirect('/actions')
else:
form = ActionForm()
form2 = ImageForm;
return render(request,'app/createForm.html', { 'form':form, 'form2':form2 })
表单模板:
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<ul>
{{ form.as_ul }}
{{ form2.as_ul }}
</ul>
<button type="submit">Save</button>
<button name="cancel" onclick="navigate({% url 'home' %})">Cancel</button>
</form>
访问页面时表单显示正常,但是提交后出现此错误:
[Errno 2] 没有那个文件或目录
它似乎不喜欢这条线:
act.action_id = form2.save()
最确定创建的模型具有我所指的所有字段,尽管我收到此错误,但前半部分 (ActionForm) 已保存到数据库中, 但不是 imageForm。一些帮助将不胜感激!
编辑
错误:
OSError at /actions/create
[Errno 2] No such file or directory
Request Method: POST
Request URL: http://127.0.0.1:8000/actions/create
Django Version: 1.8.7
Exception Type: OSError
Exception Value:
[Errno 2] No such file or directory
Exception Location: C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\core\files\storage.py in _save, line 248
Python Executable: C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\Scripts\python.exe
Python Version: 2.7.0
Python Path:
['C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2',
'C:\WINDOWS\SYSTEM32\python27.zip',
'C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\DLLs',
'C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib',
'C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\plat-win',
'C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\lib-tk',
'C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\Scripts',
'C:\Python27\Lib',
'C:\Python27\DLLs',
'C:\Python27\Lib\lib-tk',
'C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env',
'C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages']
Server time: Thu, 28 Jan 2016 15:24:04 +0000
追溯
Request Method: POST
Request URL: http://127.0.0.1:8000/actions/create
Django Version: 1.8.7
Python Version: 2.7.0
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
'django.contrib.admin',
'django.contrib.admindocs')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\app\views.py" in actioncreate
53. act.action_id = form2.save()
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\forms\models.py" in save
459. construct=False)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\forms\models.py" in save_instance
105. instance.save()
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\base.py" in save
734. force_update=force_update, update_fields=update_fields)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\base.py" in save_base
762. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\base.py" in _save_table
846. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\base.py" in _do_insert
885. using=using, raw=raw)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\manager.py" in manager_method
127. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\query.py" in _insert
920. return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
973. for sql, params in self.as_sql():
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
931. for obj in self.query.objs
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\fields\files.py" in pre_save
314. file.save(file.name, file, save=False)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\db\models\fields\files.py" in save
93. self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\core\files\storage.py" in save
63. name = self._save(name, content)
File "C:\Users\Rebecca\Documents\FINALYEAR\CSbackup\MyActionDefiner3 - 2tblWorking - Copy\MyActionDefiner2\env\lib\site-packages\django\core\files\storage.py" in _save
248. fd = os.open(full_path, flags, 0o666)
Exception Type: OSError at /actions/create
Exception Value: [Errno 2] No such file or directory
您需要指定 MEDIA_ROOT 的子目录以上传至您本地文件系统中存在的目录。检查 get_upload_file_name 函数返回的内容并检查它是否是本地文件系统上 MEDIA_ROOT(在 settings.py 中定义)的子目录:
class Image(models.Model):
image = models.FileField(upload_to=get_upload_file_name, default="")
看这里:
https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.FileField.upload_to