create form from 2 models python django: [Errno 2] No such file or directory -
i trying create form 2 models using python , django. below shows models:
# create 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)
and form classes:
class actionform(modelform): #bind form action model class meta: model = action fields = ['name','keywords', 'object', 'uploadedby', 'uploaddate'] class imageform(modelform): class meta: model= image fields =['image']
the code in views creates page createform.html template:
def actioncreate(request): if request.method == "get": #create object - actionform form = actionform; form2 = imageform; #pass return render(request,'app/createform.html', { 'form':form, 'form2':form2}) elif request.method == "post": # take of user data entered create new action instance in table form = actionform(request.post, request.files) form2 = imageform(request.post, request.files) if form.is_valid() , form2.is_valid(): #prepare action model act = form.save(commit=false) #set 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 template:
<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>
the form displays fine when page accessed, once submitted error:
[errno 2] no such file or directory
and doesnt seem line:
act.action_id = form2.save()
the models created definiely has of fields referring , although getting error first half (actionform) saves database, not imageform. appreciated!
edit
error:
oserror @ /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
traceback
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. 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. 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 @ /actions/create exception value: [errno 2] no such file or directory
you need specify sub-directory of media_root upload exists on local file system. check being returned get_upload_file_name function , check that sub-directory of media_root (defined in settings.py) on local file system:
class image(models.model): image = models.filefield(upload_to=get_upload_file_name, default="")
see here:
https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.filefield.upload_to
Comments
Post a Comment