I'm getting troubles how to make a next move in my app. In my views.py there are two functions which draw and return a non-repetitive pair of players.
views.py :
def check(who, tournament_pairs):
for fighter in tournament_pairs:
if who == fighter[0] or who == fighter[1]:
return False
return True
#If odd number of players
def who_not_fight(tournament_pairs, fighters):
final_fighters = list(itertools.chain(*tournament_pairs))
return list(set(fighters) - set(final_fighters))
where I use it:
...
fighters = PlayerTournament.objects.all().order_by('?')
all_pairs = itertools.combinations(fighters, 2)
tournament_pairs = []
for pair in all_pairs:
if check(pair[0], tournament_pairs):
tournament_pairs.append(pair)
alone_fighter = who_not_fight(tournament_pairs, fighters)
if alone_fighter:
tournament_pairs.append((alone_fighter[0], None))
And getting selected values in checkboxes:
def get_checked(request):
checked = []
if request.method == 'POST':
for key, _ in request.POST.items():
if key != 'csrfmiddlewaretoken':
checked.append(key)
print checked
return StreamingHttpResponse(','.join(checked))
Everything works nice. When I select the winners:
template.html
{% if fights %}
<form id='checked' method="POST" action="/get_checked/">
<div class="container-fluid">
{% for fighter1, fighter2 in fights %}
<div class="row">
<div class="col-md-2">
<div class="panel panel-info">
<div class="panel-body">
{% if fighter1 or fighter2 %}
<ul>
<li><input name="{{ fighter1.player_id.id }}" type="checkbox" class="checked">
{{ fighter1.player_id.name }}
{{ fighter1.player_id.surname }}
</li>
</ul>
<center>vs.</center>
<ul>
<li>
{% if fighter2 %}
<input name="{{ fighter2.player_id.id }}" type="checkbox" class="checked">
{{ fighter2.player_id.name }}
{{ fighter2.player_id.surname }}
{% else %}
BYE
{% endif %}
</li>
</ul>
{% endif %}
</div>
</div>
</div>
</div>
{% endfor %}
</div>
{% csrf_token %}
<input id="checked" type='submit' value="Save" class="btn">
</form><br>
<form id="form2" method="POST" action="/next_round/">
{% csrf_token %}
<input type="submit" value="Next round" class="btn">
</form>
{% endif %}
After clicking button I'm being redirect to /get_checked/ and I got my selected id's of checkboxes. All I wanna do is to save these values without redirecting page.
Then I tried to use Ajax:
javascript.js
$(document).ready(function () {
$("#checked").click(function () {
var input_string = $("#forminput").val();
$.ajax({
url: "/ajaxexample_json",
type: "POST",
dataType: "json",
data: {
client_response: input_string,
csrfmiddlewaretoken: '{{ csrf_token }}'
},
success: function (json) {
$('#result').append('Server Response: ' + json.server_response);
},
error: function (xhr, errmsg, err) {
alert(xhr.status + ": " + xhr.responseText);
}
});
return false;
});
});
Unfortunately, nothing happened. The next move I'd like to do is to generate the next pair of players (having selected id's of players). Also without refreshing page, just after I click the button "Next round".
Does anyone did it before or may have some ideas how would I do it in easier way ? Or maybe I got wrong somewhere in my code ?
This is how it looks: Before clicking save button After clicking save button
Thanks for each response!
Aucun commentaire:
Enregistrer un commentaire