I am writing a small test app for a bigger project. I would like to use asynchronously FileReader() to read a txt file from client side and pass the textbody to the Django server by using AJAX. When the server succeeds to get the "posted" text, it will return the length of the text. It worked well on the server and I got what I expected. But now I would like to pass the size of the text(length) back to the client and display it somewhere on the web page asynchronously. But failed... Here is my code:
HTML
<script type="text/javascript">
var render_text = function(csvFile, onLoadCallback){
var reader = new FileReader();
reader.onload = onLoadCallback;
reader.readAsText(csvFile);
}
$(document).ready(function(){
$("#id_csvFileInput").on("change", function(e){
render_text(this.files[0], function(e){
var text = e.target.result;
$.ajax({
url: "",
type: 'POST',
async: true,
data: {'text': text},
success: function(data){
$("#id_test").text(data.size);
}
});
});
});
});
</script>
<p>
<input type="file" name="csvFileInput" id="id_csvFileInput" accept=".csv">
</p>
<div>
<p>Waiting for reponse context....</p>
<span id="id_test">?</span>
</div>
View.py
# Home page.
#csrf_exempt
def home(request):
template = 'hbvapp/home.html'
context = {}
if request.method == "POST" and request.is_ajax():
context['test'] = request.POST.get('text')
context['size'] = len(context['test'])
print context['size']
return render(request, template, context)
else:
return render(request, template)
ANY HELP WILL BE DEEPLY APPRECIATED !
Reagards
try it
from django.http import JsonResponse
if request.method == "POST" and request.is_ajax():
context['test'] = request.POST.get('text')
context['size'] = len(context['test'])
print context['size']
return JsonResponse(context)
# ^^^^^
more details jsonresponse
you can send it this way also:
context['test'] = request.POST.get('text')
context['size'] = len(context['test'])
print context['size']
return HttpResponse(json.dumps(context), content_type='application/json')
and in you js you can access it like this:
success: function(data){
$("#id_test").text(data['size']);
}
Related
I'm trying to use Django Ajax to Modify user's avatar, but it doesn't work.There is no any error information, just doesn't work.
Here is my form in template:
<form class="clearfix" id="jsAvatarForm" enctype="multipart/form-data" autocomplete="off" method="post" action="{% url 'users:image_upload' %}" target='frameFile'>
<img id="avatarShow" src="{{ MEDIA_URL }}{{ request.user.image }}"/>
<input type="file" name="image" id="avatarUp" class=""/>
<button type="submit">Modify Avatar</button>
{% csrf_token %}
</form>
Here is my Ajax:
$("#jsAvatarForm").submit(function(){
var image = $("#avatarShow").val()
$.ajax({
cache: false,
type: "POST",
url:"{% url 'users:image_upload' %}",
data:{'user_pk':{{ user.pk }}, 'image':image},
async: true,
beforeSend:function(xhr, settings){
xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
},
success: function(data) {
if(data.status == 'fail'){
if(data.msg == '用户未登录'){
window.location.href="login";
}else{
alert(data.msg)
}
}else if(data.status == 'success'){
window.location.reload();//refresh current page.
}
},
});
return false;
});
Here is views.py:
class UploadImageView(LoginRequiredMixin, View):
def post(self, request):
user_pk = request.POST.get("user_pk", 0)
image = request.FILES.get('image')
user_change = UserProfile()
user_change.id = user_pk
user_change.image = image
user_change.save
return HttpResponse('ok')
Actually I also have a forms.py but I don't know how to use it with ajax:
class UploadImageForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ['image']
Here is my user model, note:I have rewrote my own USER:
class UserProfile(AbstractUser):
image = models.ImageField(upload_to="image/%Y/%m", default="image/default.png", max_length=100,verbose_name='头像')
class Meta:
verbose_name = "用户信息"
verbose_name_plural = verbose_name
def __str__(self):
return self.username
Any friend can help?Thank you so much!
Obviously You can send file such as image to django via js, depending of what you have posted in your question, I am showing you a way that you can proceed, there are lots of other ways to accomplish this fairly.
Feel free to comment, if anything is not working.
$("#jsAvatarForm").submit(function(){
var form = this;
// var image = $("#avatarShow").val() You can not retrieve the image like this
var formData = new FormData($(form)[0]);
// Everything inside the html form is serialized in the formData
// No need to add X-CSRFToken, {% csrf_token %} is inside the form
formData.append("NEW_KEY","NEW_VALUE"); # In case of further values
$.ajax({
url:$(form).attr('action'),
type:$(form).attr('method'),
data:formData,
processData: false,
contentType: false, //Don't set any content type header
success: function(){
// all your stuffs here
$('#avatarShow').attr('src',data.avatar_url)
},
error:function(){
alert("error");
}
});
return false;
});
Django
from django.http import JsonResponse
# codes here
user_change.image = image
user_change.save()
return JsonResponse({'avatar_url':user_change.image.url})
Note: Show the post view that rendering this template.
all! Can u please help me? I have a small problem. When i click button, When I click on a button, a new object should be created without reloading the page. Only one parameter is required to create an object.
The problem is that when you click the object is created (the new object is displayed in the admin panel), but in the console js there is an error:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
JS:
function initBuyButton(){
$('.button-buy').click(function(e){
e.preventDefault();
var test = $(this);
var smartphone_id = test.data("smartphone_id");
var url = test.attr("action");
basketUpdating(smartphone_id, url);
});
}
function basketUpdating(smartphone_id, url){
var data = {};
var csrf_token = $('#form_buying_product [name="csrfmiddlewaretoken"]').val();
data["csrfmiddlewaretoken"] = csrf_token;
data.smartphone_id = smartphone_id;
$.ajax({
url: url,
type: 'POST',
data: data,
cache: true,
});
}
$(document).ready(function(){
initBuyButton();
});
View:
def basket_adding(request):
"""Add new smartphone to basket."""
data = request.POST
smartphone_id = data.get('smartphone_id')
SmartphoneInBasket.objects.create(smartphone_id=smartphone_id)
return True
HTML:
<form id="form_buying_product" > {% csrf_token %}
{% for sm in smartphones %}
...
<input type="submit" action="{% url 'basket_adding' %}" class="button-
buy" data-smartphone_id = "{{ sm.id }}" value="Buy">
{% endfor %}
</form>
As mentioned in the comments, a view needs to return an HttpResponse. If you want, it can be n empty:
return HttpResponse()
You need to include the csrf token as a header.
var csrftoken = $("[name=csrfmiddlewaretoken]").val();
$.ajax({
url: url,
type: 'POST',
headers:{
"X-CSRFToken": csrftoken
},
data: data,
cache: true,
});
Because an error 500 is that your permission gets denied to send the data.
I have the following helper functions defined:
import json
def json_response(request, val, **kw):
"""Return a json or jsonp response.
"""
if request.GET.get('callback'):
return jsonp(request.GET['callback'], val, **kw)
else:
return jsonval(val, **kw)
def jsonval(val, **kw):
"""Serialize val to a json HTTP response.
"""
data = dumps(val, **kw)
resp = http.HttpResponse(data, content_type='application/json')
resp['Content-Type'] = 'application/json; charset=UTF-8'
return resp
def jsonp(callback, val, **kw):
"""Serialization with json callback.
"""
data = callback + '(%s)' % json.dumps(val, **kw)
return http.HttpResponse(
data,
content_type='application/javascript; charset=utf-8'
)
with those defined your view can return a json object (to your ajax call):
def basket_adding(request):
"""Add new smartphone to basket."""
...
return json_response(request, True)
it's common practice to return an object though, so perhaps:
return json_response(request, {"status": True})
According to the documentation of django all-auth , it supports logging in through AJAX requests.When I make a normal post request to "accounts/login/" ,content type of the response header is "text/html".
But when I make a ajax call it is "application/json".
I am unable to figure what I am doing wrong, I have tried changing the contentType and dataType in ajax call but it gives a 400 Bad request error.
I have not modified any URL or view of the default Django all-auth app.
I am including the JavaScript code here -
<script type="text/javascript">
var $button = $('#login_button');
$button.on('click',function(){
var data = {"csrfmiddlewaretoken" : document.getElementsByName('csrfmiddlewaretoken')[0].value,
"login": $('#id_login').val(),
"password": $('#id_password').val(),
"remember": $('#id_remember').val() };
var temp = {'X-CSRFToken': document.getElementsByName('csrfmiddlewaretoken'[0].value };
$.post({
url : "{% url 'account_login' %}",
headers: temp,
type: "POST",
data : data,
contentType: "application/x-www-form-urlencoded",
dataType: "text",
success : function(data) {
// console.log(data);
},
});
});
</script>
it works for me, try:
$.post("{% url 'account_login' %}", {
"csrfmiddlewaretoken" : document.getElementsByName('csrfmiddlewaretoken')[0].value,
"login": $('#id_login').val(),
"password": $('#id_password').val(),
"remember": $('#id_remember').val()
},
function (data, status) {
// console.log(data);
});
I had similar issue and for me worked this solution (not a nice way but worked):
- in adapter.py you can see if form is not valid, status 400 and i have also changed the data
def ajax_response(self, request, response, redirect_to=None, form=None):
data = {}
status = response.status_code
if redirect_to:
status = 200
data['location'] = redirect_to
if form:
if form.is_valid():
status = 200
else:
status = 200
data['form_errors'] = form._errors
if hasattr(response, 'render'):
response.render()
data = response.content
return HttpResponse(json.dumps(data),
status=status,
content_type='application/json')
Here is how I used AJAX with vanilla JavaScript to login to django-allauth.
The django-allauth AJAX response does not include form errors, so I created a new class-based view based on Adam Starrh's solution:
from django.http import JsonResponse
from allauth.account.views import LoginView
class AjaxLoginView(LoginView):
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
response = self.form_valid(form)
return JsonResponse({'loggedIn': True})
else:
return JsonResponse({'loggedIn': False, 'errors': form.errors})
In urls.py I added a path to this view:
path('login-with-ajax/', AjaxLoginView.as_view(), name='loginWithAjax')
The function that makes the AJAX request does the following:
Create a new form with FormData() and add the login and password.
Encode the form with a helper method.
Use XMLHttpRequest() to post the form, using content type "application/x-www-form-urlencoded".
Here is the method I used:
loginWithAjax(submittedEmail,submittedPassword){
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest()
let form = new FormData()
form.set('login', submittedEmail)
form.set('password', submittedPassword)
xhr.open('POST', '/accounts/login-with-ajax/')
xhr.setRequestHeader('Content-Type', "application/x-www-form-urlencoded")
xhr.setRequestHeader("X-CSRFToken", this.getCookie("csrftoken"))
xhr.onload = () => {
if (xhr.status === 200) { //received an ajax response from view
let ajaxResponse = {
loggedIn: false,
error: ''
}
let viewResponse = JSON.parse(xhr.response)
ajaxResponse.loggedIn = viewResponse.loggedIn
if (!ajaxResponse.loggedIn){
ajaxResponse.error = viewResponse.errors.__all__[0]
}
resolve(ajaxResponse)
} else { //did not receive an ajax response from view
reject(xhr.status)
}
}
xhr.send(this.urlencodeFormData(form))
})
}
I used the method provided by cuixiping to encode the form:
urlencodeFormData(fd){
var s = '';
function encode(s){ return encodeURIComponent(s).replace(/%20/g,'+'); }
for(var pair of fd.entries()){
if(typeof pair[1]=='string'){
s += (s?'&':'') + encode(pair[0])+'='+encode(pair[1]);
}
}
return s;
}
I hope this saves someone some time in the future.
I want to make an ajax request in a Django framework. However, I don't pass to get data from the client in json. It works when I don't use Json.
If I use dataType:'json' with a {'a': 'value'} in the ajax, I can't get it in the view.py, the result is nothing...
However if I use data:$(this).serializeArray() in the ajax I can get result with request.POST. However, I really need to customize my data and send to my view.py other data than the data from the form. I would like to send a {'a', 'mydata', 'form': myformdata}...
Is there a way to do it?
template:
<form id="ajax2" action="/seghca/test-post/" method="post">{% csrf_token %}
Nom : <input type="text" name="nom" value="" id="nom"/><br/>
prenom : <input type="text" name="prenom" value=""/><br/>
<input type="submit" value="Envoyer"/>
</form>
<div id="result"></div>
javascript:
$(document).ready(function(){
// POST AJAX
$("#ajax2").submit( function() {
var urlSubmit = $(this).attr('action');
var data = $(this).serializeArray();
data.push({
key: "keyName",
value: "the value"
});
$.ajax({
type: "POST",
url: urlSubmit,
dataType: "json",
data : data,//$(this).serializeArray(),
success: function(response){
var json_response = JSON.parse(response);
// now get the variables from the json_response
$('#result').html(json_response.html);
}
});
return false;
});
});
view.py (the ajax launch the test_post view, home2 is the view of the formular):
from datetime import datetime
from django.http import HttpResponse, Http404
from django.shortcuts import redirect, render
from seghca.models import Article
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt
import json
def home2(request):
return render_to_response('seghca/form.html', context_instance=RequestContext(request))
#csrf_exempt
def test_post(request):
data = {'html': request.POST['key']}
return HttpResponse(json.dumps(data), mimetype="application/json")
When you are using ajax view you should return the data back from your view in the json form:
data = {'html': request.POST['input']}
return HttpResponse(json.dumps(data), mimetype="application/json")
Second it is necessary to parse the response first on client side:
success: function(response){
var json_response = JSON.parse(response);
// now get the variables from the json_response
$('#result').html(json_response.html);
}
Third if you need to pass the form data along with some more info you can do:
var data = $(this).serializeArray();
data.push({
key: "keyName",
value: "the value"
});
Fourth you are missing csrf token.
change data: data, to data: {'data': JSON.stringify(data)},
and you'll be able to access the serialized version of your data via POST['data'] in django. Keep in mind that if you want to use this in django you have to deserialize it, for instance json.loads(POST['data'])
I have your same needs. My solution was:
The AJAX request:
var posturl = $('#'+formid).prop('action');
$.ajax({
async:false,
type: "POST",
dataType: "json",
contentType: "application/x-www-form-urlencoded",
url : posturl,
data : $('#'+formid).serialize() + '&mode=ajax', //&mode=ajax is my custom data
success:function(response){
console.log(response);
alert(response.message);
},
timeout:10000
});
In the views.py:
data = {'error': '0', 'message': 'all was ok'}
return HttpResponse(json.dumps(data), mimetype="application/json")
The above should work for you. My test was with Django 1.6 and Python 2.7.5
Looking to get some insight and tips on using ajax with django.
Say I have a function:
def add_comment(request, pk):
if request.method == 'POST' and request.is_ajax():
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
comment = comment_form.save(commit=True)
comment.save()
json = simplejson.dumps(comment, ensure_ascii=False)
return HttpResponse(json, mimetype='application/json')
return render_to_response({{ post.id }}', {'comment': comment,}), context_instance=RequestContext(request), mimetype='application/json')
and I'm trying to post the comments to a page without a redirect with ajax function:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script></javascript>
<script type="text/javascript">
$(document).click(function()
{
$('#comment_form').submit(function()
{
var dataString = $('#comment_form').serialize();
$.ajax({
type: 'POST',
url: '',
data: dataString,
success: function(data){
$('').html(data);
},
});
return false;
});
});
</script>
I believe I'm mixing up a few things here. I am trying to get the page to load comments without a redirect. I don't need an exact answer, maybe just steered in the right direction.
This can helps:
this could be your view:
import json
def add_comment(request, pk):
if request.method == 'POST' and request.is_ajax():
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
comment = comment_form.save(commit=True)
comment.save()
json_response = json.dumps({"status":"Success"})
return HttpResponse(json_response)
errors = {}
for k, v in job_type_form.errors.items():
errors[k.capitalize()] = v
response = {
'success': False,
'errors': errors
}
return HttpResponse(json.dumps(response))
and your jquery could be like this:
$('#comment_form').submit(function() {
var dataString = $('#comment_form').serialize();
$.ajax({
type: 'POST',
url: '',// you need to put this to something like '{% url to_your_view %}'
data: dataString,
dataType: 'json'
success: function(data){
// you can access to your json object like data.status or data.something
$('').html(data.status);
},
});
return false;
});
Thanks for the posts I finally got things worked out. The jquery was the main issue.
$(document).ready(function() {
$('#comment_form').submit(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: '{% url art.views.post %}',
data: $('#comment_form').serialize(),
dataType: 'json';
success: function(){
location.reload();
$('#comment_form').get(0).reset();
},
});
return false;
});
});
I was sending the DOM object not the actual form data to the view.
In the view I combined two functions to get the two sharing the same URL.
def post(request, pk):
post = Post.objects.get.(pk=int(pk))
comments = Comment.objects.filter(post=post)
_dict = dict(post=post, comments=comments, form=Comment_form(), user=request.user)
_dict.update(csrf(request))
cf_obj = Comment(post = Post.objects.get(pk=pk))
if request.method == 'POST' and request.is_ajax():
if comment_form.is_valid():
comment = comment_form.save(commit=True)
else:
raise Http404
response = serializers.serialize('json', [comment])
return HttpResponse(response, mimetype='application/json')
return render_to_response('post.html', d)