html
{% extends 'base.html' %}
{% block script %}
{% endblock script %}
{% block content %}
<form action="{% url 'base' %}" method="post">
{% csrf_token %}
{{ game_sorted_form }}
</form>
<div id="result">
{% for game in games %}
{% if game.game_currency == False %}
<p><a href="{{ game.accounts_absolute_url }}">{{ game.name }}</a></p>
{% else %}
<p><a href="{{ game.currency_absolute_url }}">{{ game.name }}</a></p>
{% endif %}
<br>
{% endfor %}
</div>
{% endblock content %}
forms.py
class GamesSortedForm(forms.Form):
s = list()
q = Games.objects.all().order_by('name')
for a in q:
if (f'{a.name}'[0], f'{a.name}'[0]) in s:
pass
else:
s.append((f'{a.name}'[0], f'{a.name}'[0]))
name = forms.ChoiceField(choices=s,
widget=forms.Select(
attrs={'class': 'form-select', 'name': 'form-select', 'id': 'form-select'}),
label=False,
required=False)
views.py
class BaseView(ContextBase, ContextList):
model = Games
context_object_name = 'games'
template_name = 'Pay/games_list.html'
queryset = Games.objects.all().order_by('name')
utils.py
class ContextBase(ListView):
def post(self, request):
if request.method == 'POST':
form = GamesSortedForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
queryset = self.queryset
list_game_id_num = []
for i in queryset:
if f'{i}'[0] == name:
list_game_id_num.append(i.id_num)
context = {
'games': Games.objects.filter(id_num__in=list_game_id_num).order_by('name'),
'game_sorted_form': GamesSortedForm(),
'user_name': Customer.objects.get(user=self.request.user)
}
return render(request, 'Pay/games_list.html', context)