There are two HTML templates - swiss.html and swiss_result.html, on each form with data with the method post (method = “post”), the forms are the same, BUT from the first form, POST transmits the full name to the view (which corresponds to the bill_fio field in the model (class attribute)), i.e. 'Petrov' FROM the second template, when it is re-accessed, POST sends the full name data with the prefix of the model name to the view? those. (name Bill, - class Bill (models.Model) :) the model is the same, the patterns are identical, but why is this happening?

it is necessary to transfer only the full name to the view, without the prefix

by code:
from f-ii def swiss () in the view

def swiss(request): args = {} args.update(csrf(request)) index = Bill.objects.all() nov_por = Bill.objects.filter().order_by('-bill_mat_ozh') a = nov_por.count() // 2 nov_por_1 = (nov_por[:a]) nov_por_2 = (nov_por[a:]) res_1 = [(nov_por_1[n].bill_fio) for n in range(nov_por_1.count()) if nov_por_1[n] in nov_por_1] res_2 = [(nov_por_2[n].bill_fio) for n in range(nov_por_2.count()) if nov_por_2[n] in nov_por_2] players = zip_longest(res_1, res_2) return render_to_response('swiss.html', {'players':players, 'index':index}, context_instance=RequestContext(request)) 

data is transferred to the swiss.html template:

 <form action="{% url 'first_step' %}" method="post"> {% csrf_token %} <table border="1"> {% for opponents_player in players %} {% if None not in opponents_player %} <tr> <td> {% for single_player_if_yes in opponents_player %} {{ single_player_if_yes }} <br> {% endfor %} </td> <td> {% for single_player in opponents_player %} <input type="radio" name="{{ opponents_player }}" value="{{ single_player }}">{{ single_player }} <br> {% endfor %} <input type="radio" name="{{ opponents_player }}" value="no">Ничья<br> </td> </tr> {% else %} <tr> <td> {% for single_player_if_none in opponents_player %} {% if single_player_if_none != None %} {{ single_player_if_none }} {% endif %} {% endfor %} </td> <td> {% for single_player in opponents_player %} {% if single_player != None %} <input type="radio" name="{{ opponents_player }}" value="{{ single_player }}" checked>{{ single_player }} <br> {% endif %} {% endfor %} </td> </tr> {% endif %} {% endfor %} </table> <br> <input type="submit" value="Play"> </form> 

then after clicking on the Play button, the data from the template goes to the function first_step ()

 def first_step(request): game_data = request.POST print(game_data) for key, value in game_data.items(): if key == 'csrfmiddlewaretoken': pass elif value == 'no': split_player = key.split(', ') for player in split_player: player_name = re.sub(r'[^\w\s-]+', r'', player).strip() standoff = Bill.objects.get(bill_fio = player_name) standoff.swiss_bill_score = standoff.swiss_bill_score + 0.5 standoff.save() else: bill = Bill.objects.get(bill_fio = value) bill.swiss_bill_score = bill.swiss_bill_score + 1 bill.save() for key, value in game_data.items(): if key == 'csrfmiddlewaretoken': pass else: spisok = [] if 'None' in key: pass else: wr_split_player = key.split(', ') for rw_player in wr_split_player: rw_player_name = re.sub(r'[^\w\s-]+', r'', rw_player).strip() rw_player_id = Bill.objects.get(bill_fio = rw_player_name).id spisok.append(rw_player_id) spisok.sort() length = len(spisok) if length == 2: rival_1 = Bill.objects.get(id = spisok[0]) rival_2 = Bill.objects.get(id = spisok[1]) rival_1.swiss_rivel = spisok[1] rival_2.swiss_rivel = spisok[0] rival_1.save() rival_2.save() else: pass swiss_players = Bill.objects.all().order_by('-swiss_bill_score') new_game = swiss_players.count() // 2 new_group_1 = (swiss_players[:new_game]) new_group_2 = (swiss_players[new_game:]) id_new_group_1 = [] id_new_group_2 = [] for n1 in new_group_1: igrok = Bill.objects.get(bill_fio = n1) id_igrok = igrok.id id_new_group_1.append(id_igrok) for n2 in new_group_2: igrok = Bill.objects.get(bill_fio = n2) id_igrok = igrok.id id_new_group_2.append(id_igrok) result_finish = [] for single_id in id_new_group_1: result = [single_id, random.choice(id_new_group_2)] print(request) if Bill.objects.get(id=result[0]).swiss_rivel is not None: while str(result[1]) in Bill.objects.get(id=result[0]).swiss_rivel: result = [single_id, random.choice(id_new_group_2)] break else: result = [single_id, random.choice(id_new_group_2)] id_new_group_2.remove(result[1]) result_2 = [Bill.objects.get(id=result[0]), Bill.objects.get(id=result[1])] result_finish.append(result_2) if id_new_group_2 != None: end_player = [None, Bill.objects.get(id=id_new_group_2[0])] result_finish.append(end_player) return render_to_response('swiss_result.html', {'swiss_players':swiss_players, 'result_finish':result_finish}, context_instance=RequestContext(request)) 

which in turn passes the values ​​and opens the swiss_result.html template

 <form action="{% url 'first_step' %}" method="post"> {% csrf_token %} <table border="1"> {% for opponents_player in result_finish %} {% if None not in opponents_player %} <tr> <td> {% for single_player_if_yes in opponents_player %} {{ single_player_if_yes }} <br> {% endfor %} </td> <td> {% for single_player in opponents_player %} <input type="radio" name="{{ opponents_player }}" value="{{ single_player }}">{{ single_player }} <br> {% endfor %} <input type="radio" name="{{ opponents_player }}" value="no">Ничья<br> </td> </tr> {% else %} <tr> <td> {% for single_player_if_none in opponents_player %} {% if single_player_if_none != None %} {{ single_player_if_none }} {% endif %} {% endfor %} </td> <td> {% for single_player in opponents_player %} {% if single_player != None %} <input type="radio" name="{{ opponents_player }}" value="{{ single_player }}" checked>{{ single_player }} <br> {% endif %} {% endfor %} </td> </tr> {% endif %} {% endfor %} </table> <br> <input type="submit" value="Play"> </form> 

and here again pressing on Play, again gives the data of the function f_i to first_step in the view and there already go the names with the prefix of the model name. Why is that? how to get rid of it? whatever the number of calls to this form, she gave only the full name, without prefixes

  • Give an example of patterns and views. - MichaelPak

1 answer 1

decided:
1. initially not quite correctly put it into the <class 'itertools.zip_longest'> template, transformed it into <class 'list'>
2. in the view, in the f- def first_step found that it added the prefix as a model name, these turned out to be strings

 result_2 = [Bill.objects.get(id=result[0]), Bill.objects.get(id=result[1])] ... end_player = [None, Bill.objects.get(id=id_new_group_2[0])] 

added the str() method, i.e.

 result_2 = [str(Bill.objects.get(id=result[0])), str(Bill.objects.get(id=result[1]))] ... end_player = [None, str(Bill.objects.get(id=id_new_group_2[0]))] 

although it is not entirely clear why this had to be done, since the model already states that it should display a field named

 class Bill(models.Model): ... bill_fio = models.CharField(max_length=50, verbose_name='ФИО') ... def __str__(self): return "%s" % self.bill_fio 

ps: but in general was chosen not quite the right way to send to the data template, because do it by sending objects to the template