image - django vanilla views create instance with imagefield using CreateView -
image - django vanilla views create instance with imagefield using CreateView -
i using django vanilla views (https://github.com/tomchristie/django-vanilla-views) edit model object (the model createview), model has imagefield.
but on create not seem save image, maybe because not standard usage.
the few things:
for model imagefield using upload_to function save image specific dir the create done info not filled in user, using dispatch related object url before saving, in form_valid function, utilize form.save(commit=false) things before saving , afterwards save objectthe code:
the function used upload_to
def get_image_path_albumphoto(instance, filename): homecoming os.path.join('albums', slugify(str(instance.album)), filename)
the model (at to the lowest degree important fields):
class albumphoto(models.model): .... fields ... album = models.foreignkey(album, blank=false, null=false) image = models.imagefield(upload_to=get_image_path_albumphoto, blank=true, null=true)
the create view:
class albumphotocreate(createview): model = albumphoto fields=('all other fields except album','image') def dispatch(self, *args, **kwargs): self.album = get_object_or_none(album, id=kwargs['album_id']) homecoming super(albumphotocreate, self).dispatch(*args, **kwargs) def get_context_data(self, **kwargs): kwargs['album'] = self.album homecoming kwargs def get_form(self, data=none, files=none, **kwargs): initial={'some_field':'gets_initialized here'} kwargs['initial']=initial homecoming super(albumphotocreate, self).get_form(data,files, **kwargs) def get_success_url(self): if self.album: homecoming 'an url using album id %d' % self.album.id homecoming reverse_lazy('albumphoto_list') def form_valid(self, form): obj = form.save(commit=false) obj.album=self.album obj.save() success_url= 'some url object id %d' % obj.id homecoming httpresponseredirect(success_url)
but image never saved using code.... works when using django admin add together object,,, it's in using createview
update
i tried reverse code first include album in fields, removed dispatch , form_valid functions... no succes... intialising removed... (get_form) no succes well... , parctically default usage of createview... it's in upload_to function... (?)
the reply problem more generic question formed:
the vanilla update view extended version of genericmodelview, problem more generic, , not caused django vanilla view.
to upload file using form view, form tag in template should have:
the enctype="multipart/form-data"
e.g.:
<form method="post" action="." enctype="multipart/form-data">
django image forms
Comments
Post a Comment