Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ЕСТЬ ВОТ ТАКАЯ ФОРМОЧКА
- <form action="" method="post" id="post-form">
- {% for answer in answers %}
- <input type="radio" id="{{ answer.pk }}" name="answer" value="{{ answer.answer_text }}">
- <label for="{{ answer.pk }}">{{ answer.answer_text }}</label><br>
- {% endfor %}
- <button type="submit" class='button'>Next question</button>
- {% csrf_token %}
- </form>
- ЕСТЬ ВОТ ТАКОЙ АЖАКС
- $(document).on('submit', '#post-form',function(e){
- let id = $('input[type=radio][name=answer]:checked').attr('id');
- console.log();
- // Тут мы делаем +1 к куррент квещну и показываем его
- alert(id);
- $.ajax({
- url : "{% url 'game:quiz_detail' 1 %}", // the endpoint
- type : "POST", // http method
- data : { the_post : 'sskdakdas' }, // data sent with the post request
- // handle a successful response
- success : function(json) { // remove the value from the input
- console.log(json); // log the returned json to the console
- console.log("success"); // another sanity check
- },
- // handle a non-successful response
- error : function(xhr,errmsg,err) {
- $('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: "+errmsg+
- " <a href='#' class='close'>×</a></div>"); // add the error to the dom
- console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
- }
- });
- И ТАКОЙ КУСОК ВЬЮХИ
- ПОЧЕМУ В ЗАПРОС НЕ ПОПАДАЕТ ВОТ ЭТО data : { the_post : 'sskdakdas' }, // data sent with the post request И ВООБЩЕ
- КАК БУДТО НЕ ОТПРАВЛЯЕТСЯ АЖАКС ЗАПРОС, А ОТПРАЛВЯЕТСЯ ОБЫЧНЫЙ ПОСТ ЗАПРОС С ФОРМЫ??
- if request.method == 'POST':
- response_data = {}
- print(request.body)
- s = request.POST.get('the_post')
- response_data['su'] = '2ddf'
- response_data['dasdas'] = s
- return HttpResponse(
- json.dumps(response_data),
- content_type="application/json"
- )
Add Comment
Please, Sign In to add comment