Django - How to Save ImageField Even if Form is Not Valid -


i have standard model , form. have mandatory fields imagefield. when user choose image , doesn't field mandatory fields, image isn't saved , user needs 're-upload' again.

as row in database exists (because form part of 'wizard'), don't mind saving picture in database if form isn't valid mandatory data.

this have right in view, works when fill mandatory fields:

def my_view(request):     instance = mymodel.objects.get(user=request.user)     form = myform(instance=instance)      if request.post:         form = myform(request.post, request.files, instance=instance)         if form.is_valid():             new_instance = form.save(commit=false)              if request.files , request.files['photo']:                 uploaded_photo = request.files['photo']                 new_instance.photo = uploaded_photo              new_instance.save()             return httpresponseredirect(reverse('new_url'))      return render_to_response('current_template.html', locals(), context_instance=requestcontext(request)) 

here's tried save picture in db if other fields aren't filled, error django upload error - upload valid image (either not image or corrupted):

def my_view(request):     instance = mymodel.objects.get(user=request.user)     form = myform(instance=instance)      if request.post:         form = myform(request.post, request.files, instance=instance)          if request.files , request.files['photo']:             uploaded_photo = request.files['photo']             instance.photo = uploaded_photo             instance.save()          if form.is_valid():             new_instance = form.save()             return httpresponseredirect(reverse('new_url'))      return render_to_response('current_template.html', locals(), context_instance=requestcontext(request)) 

here's form (fairly simple):

class myform(modelform):     first_name = forms.charfield(label='first name', max_length=50, required=true)     last_name = forms.charfield(label='last name', max_length=50, required=true)     photo = forms.imagefield(required=false)      def __init__(self, *args, **kwargs):         super(myform, self).__init__(*args, **kwargs)      class meta:         model = mymodel         fields = ('first_name','last_name', 'photo') 

here's model (again simple):

class mymodel(models.model):     first_name = models.textfield(max_length=50)     last_name = models.textfield(max_length=50)     photo = imagefield(upload_to=get_photo_path,null=true,blank=true) 

this how made work. notice there's no 'request.files' parameter constructor of form in 'else' when form not valid. made django display error message.

if form.is_valid():     instance = form.save(commit=false)      if request.files , request.files['photo']:         instance = save_photo(instance, request.files['photo'])      instance.save()     return httpresponseredirect(reverse('url')) else:     if request.files , request.files['photo']:         instance = save_photo(instance, request.files['photo'])         form = instanceform(request.post, instance=instance) 

Comments

Popular posts from this blog

r - how do you merge two data frames the best way? -

How to debug "expected android.widget.TextView but found java.lang.string" in Android? -

php - mySQL problems with this code? -