I'm working on a image upload form. When the user uploads an image. Display that image with ajax in the preview area... The form reloads but it seems like it doesn't want to show the new avatar. And I get a CSRF verification failed. Request aborted error.
Reason given for failure: CSRF is missing or incorrect.
I have a csrf_token in place within the form.
Template:
<form class="nice inline-form" enctype="multipart/form-data" method="POST" action="/profile/edit/" id="avatarLoadForm">
<input type="hidden" name="next" value="/account/settings/">
{% csrf_token %}
<div>
<label for="avatar">Avatar</label>
<div id="preview" class="frame">
<img src="{% if profile.user %}{% thumbnail profile.avatar 120x120 crop %}{% else %}{{ DEFAULT_AVATAR }}{% endif %}" alt="" alt="sample-pic" id="thumb" />
</div>
<input type="file" size="20" id="imageUpload">
and other form info...
</form>
Ajax/Jquery:
$(document).ready(function(){
var thumb = $('img#thumb');
new AjaxUpload('imageUpload', {
action: $('#avatarLoadForm').attr('action'),
name: 'avatar',
csrfmiddlewaretoken: $( "#csrfmiddlewaretoken" ).val(),
onSubmit: function(file, extension) {
$('#preview').addClass('loading');
},
onComplete: function(file, response) {
thumb.load(function(){
$('#preview').removeClass('loading');
thumb.unbind();
});
thumb.attr('src', response);
}
});
Maybe there's something I'm missing in my Views.py?
@login_required
def edit_profile(request):
context = base_context(request)
if request.method == 'POST':
notify = "You have successfully updated your profile."
user_info_form = UserInfoForm(request.POST, request.FILES)
if user_info_form.is_valid():
if request.is_ajax():
response = simplejson.dumps({"status": "Upload Success"})
return HttpResponse (response, mimetype='application/json')
messages.success(request, 'You have successfully updated your profile.')
user_info_form.save(request.user, profile_type)
return HttpResponseRedirect(request.POST.get('next', '/profile/' + request.user.username + '/'))
else:
initial = {}
initial['first_name'] = request.user.first_name
initial['last_name'] = request.user.last_name
initial['email'] = request.user.email
initial['about'] = profile.about
initial['country'] = profile.country
initial['about'] = profile.about
user_info_form = UserInfoForm(initial=initial)
context['user_info_form'] = user_info_form
context['profile'] = profile
return render_to_response('settings/profile.html', context, context_instance=RequestContext(request))
Settings.py:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'pagination.middleware.PaginationMiddleware',
)
The file upload works without the ajax crap. As to say, it actually saves the image, just doesn't show it in the preview. I really don't know what's wrong or why this is happening. There's been alot of posts regarding csrf failures. But I can't find one that pertains to this situation. Any insight would be very much appreciated.