javascript - Django and Ajax - Implementing an Array from Ajax to the template -
currently i'm trying implement ajax django in order refresh sections of web app. i'm having trouble in 2 parts.
- first getting value input template views.py. seems when passes , print out in cmd sure value passed correctly,
none
. i'm not sure why going in that. know managed pass correctly when directly views.py without going in javascript file first. - secondly don't know how implement view.py returns
ajax
function in javascript file. mean when whole instruction set in views , return informaation needed ajax, dont know command use implement in template. currenly i'm returning array of chars changes order depending on user input. @ first loading, alphabet in order when user enters number, letters shift according input. got part going i'm stuck @ taking template after been shifted.
also recommendation welcome. if see not right done or implemented in better way, please let me know. code ethics highly appreciated!
my code here:
forms.py
class caesarcipher(forms.form): key = forms.decimalfield(max_value = 26, min_value = 1, initial = 1, required = true) plaintext = forms.charfield(max_length = 8, required = true) letter = forms.charfield(max_length = 1)
views.py
def caesarhomepage(request): cipher = alphabet() x = cipher.getlistletter() y = cipher.getlistletter() if request.method == 'post': form = caesarcipher(request.post or none) print(request.post.get('id_key')) #this gives none print(form['key'].value()) # gives none int(form['key'].value()) y = cipher.setcipherletters(integerkey) return httpresponse(json.dumps({'y' : y}), content_type="application/json") form = caesarcipher(request.post or none) context = { 'x': x, 'y': y, 'form': form, } return render(request, 'homecaesar.html', context)
main.js
$("#keybuttonid").on({ click : function() { $.ajax( { url : "http://127.0.0.1:8000/homecaesar/", type : "post", data: { csrfviewmiddleware: '{{ csrf_token }}', the_key: $('#id_key').val() }, success : function(json) { // $('#id_key').val('0'); console.log(json); $('#alteredalphabet').append(json); // not work, need change console.log("function called!"); } }); }
});
the first part of problem has nothing ajax. not how access form data. need check form.is_valid(), access data through
form.cleaned_data`.
Comments
Post a Comment