0

My model works good when page makes POST request but when it makes it makes ajax request it stops saving an image( I can not change image to another), rest fields works good. What can be the problem? Here is my model:

class Contact(models.Model):
    name = models.CharField(max_length=40, blank=False)
    last_name = models.CharField(max_length=40, blank=False)
    date_of_birth = models.DateField(blank=True, null=True)
    bio = models.TextField(blank=True)
    contacts = models.TextField(blank=True)
    email = models.EmailField(blank=True)
    jabber = models.CharField(max_length=40)
    skype = models.CharField(max_length=40)
    other_contacts = models.TextField(blank=True)
    photo = models.ImageField(upload_to='photo/', null=True)

Ajax function:

$(function(){
    var form = $('#contact_form');
    form.submit(function(e){
        $('#sendbutton').attr('disabled', true)
        $('#ajax-progress-indicator').show();
        $('#ajaxwrapper').load('/1/edit/' + '#ajaxwrapper',
            form.serializeArray(), function(responseText, responseStatus) {
                $('sendbutton').attr('disabled', false)
                $('#ajax-progress-indicator').hide();
            });
        e.preventDefault();
    });

});

Urls:

urlpatterns = patterns(
    '',
    # Examples:
    url(r'^$', 'apps.hello.views.home', name='home'),    
)

from .settings import MEDIA_ROOT, DEBUG


if DEBUG:
# serve files from media folder
        urlpatterns += patterns('',
                url(r'^uploads/(?P<path>.*)$', 'django.views.static.serve', {
                        'document_root': MEDIA_ROOT}))

Form:

class ContactForm(forms.ModelForm):
    """
    The model for Contact model edit
    """
    # other_contacts = forms.CharField(widget=PreformattedTextWidget)
    class Meta:
        model = Contact
        fields = ['name', 'last_name', 'date_of_birth', 'bio', 'contacts', 
            'email', 'jabber', 'skype', 'other_contacts', 'photo']
        widgets = {
            'skype': forms.Textarea(attrs={'placeholder': 'Enter your skype ID'}),
            # 'other_contacts': PreformattedTextWidget()
        }

View class:

class ContactUpdateView(UpdateView):
    model = Contact
    form_class = ContactForm
    template_name = 'edit.html'

    def get_success_url(self):
        return reverse('home')


    def post(self, request, *args, **kwargs):
        if request.POST.get('cancel_button'):
            return HttpResponseRedirect(
                u'%s?status_message=Canceled!'
                    %reverse('home'))
        else:
            return super(ContactUpdateView, self).post(request, *args,
                     **kwargs)
    def get_ajax(self, request, *args, **kwargs):
        return render(request, 'home.html')
kapitoshka
  • 45
  • 1
  • 10
  • show view function or class, please – vadimchin Dec 25 '15 at 11:54
  • 1
    Solution can be found here http://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax or here http://stackoverflow.com/questions/6974684/how-to-send-formdata-objects-with-ajax-requests-in-jquery – SomethingElse Dec 25 '15 at 12:39

0 Answers0