Answer the question
In order to leave comments, you need to log in
Question about Django. Assistance in the implementation of registration for the tournament. How to implement?
At the moment there are such models:
class Team(models.Model):
name = models.CharField('Название', max_length=35, unique=True)
tag = models.CharField('Тег', max_length=16, unique=True)
about = models.TextField('О команду', max_length=500, null=True, blank=True)
logo = models.ImageField('Лого', upload_to="teams_logo/", null=True)
game = models.ForeignKey(
Game, verbose_name='игра', on_delete=models.SET_NULL, null=True, blank=True
)
tournament = models.ManyToManyField('Tournaments', verbose_name='Турниры', blank=True)
slug = models.SlugField(unique=True, blank=True, null=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("team_detail", kwargs={"slug": self.slug})
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Team, self).save(*args, **kwargs)
class Meta:
verbose_name = "Команда"
verbose_name_plural = "Команды"
class Tournaments(models.Model):
name = models.CharField('Название турнира', max_length=50)
description = models.TextField('Описание турнира')
prize = models.TextField('Призовой')
game = models.ForeignKey(
Game, verbose_name='Дисциплина', on_delete=models.CASCADE
)
author = models.ForeignKey(
User, verbose_name='пользователь', on_delete=models.CASCADE, blank=True, null=True
)
teams = models.ManyToManyField(
Team, verbose_name='Команда', blank=True
)
image = models.ImageField('Лого турнира')
max_teams = models.PositiveSmallIntegerField('Максимальное количество команд', default=0)
count_registration_teams = models.PositiveSmallIntegerField('Количество зарегестрированных команд', default=0)
start_date = models.DateTimeField("Дата начала")
start_registration_date = models.DateTimeField("Начало регистрации")
end_registration_date = models.DateTimeField("Конец регистрации")
slug = models.SlugField(unique=True, blank=True, null=True)
status = models.BooleanField('Статус активности', default=False)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse("tournament_detail", kwargs={"slug": self.slug})
def save(self, *args, **kwargs):
self.slug = slugify(f'{self.name} - {self.game}')
super(Tournaments, self).save(*args, **kwargs)
def get_tournament(self):
return self.tournamentregistration_set
class Meta:
verbose_name = "Турнир"
verbose_name_plural = "Турниры"
class TournamentRegistration(models.Model):
tournaments = models.ForeignKey(Tournaments, on_delete=models.CASCADE)
teams = models.ForeignKey(Team, on_delete=models.CASCADE, null=True, blank=True)
user = models.ForeignKey(
User, verbose_name='пользователь', on_delete=models.CASCADE, blank=True, null=True
)
class TournamentsRegistration(LoginRequiredMixin, View):
def post(self, request, pk, *args, **kwargs):
print(f'TYT ----> {request.POST} & {request.GET}<----')
form = TournamentsRegistrationForm(request.POST or None)
tournaments = Tournaments.objects.get(id=pk)
if form.is_valid():
print("OK")
form.save(commit=False)
form.tournaments = tournaments
form.teams = request.user.profile.team
form.tournaments.count_registraion_teams += 1
form.save()
else:
print('НЕВЕРНАЯ ФОРМА')
print(form.errors)
print(form.non_field_errors())
return redirect(tournaments.get_absolute_url())
class TournamentsRegistrationForm(forms.ModelForm):
class Meta:
model = TournamentRegistration
fields = {'tournaments', 'teams', 'user'}
<div class="registered_teams">
<div class="registered_teams">
{{ tournaments.count_registration_teams }} / {{ tournaments.max_teams }}<br>
</div>
<div class="tournament-registration">
<form action="{% url 'tournament_registration' tournaments.id %}" method="post">
{% csrf_token %}
<button type="submit">Зарегестрироваться</button>
</form>
</div>
</div>
class TournamentsRegistration(LoginRequiredMixin, View):
def get(self, request, pk):
print(f'TYT ----> {request.POST} & {request.GET}<----')
form = TournamentsRegistrationForm(request.POST or None)
tournaments = Tournaments.objects.get(id=pk)
context = {'tournaments': tournaments, 'form': form}
return render(request, 'tournaments/tournaments_detail.html', context)
def post(self, request, tournament_id, *args, **kwargs):
print(f'TYT ----> {request.POST} & {request.GET}<----')
form = TournamentsRegistrationForm(request.POST or None)
tournaments = Tournaments.objects.get(id=tournament_id)
if form.is_valid():
print("OK")
form.save(commit=False)
form.tournaments = tournaments
form.tournaments.teams.add(request.user.profile.team)
form.user = request.user
form.tournaments.count_registration_teams += 1
request.user.profile.team.tournament.add(tournaments)
form.save()
print(form.tournaments.teams.all())
print(form.tournaments.teams.filter(tournaments__teams__name=request.user.profile.team.name))
return redirect(tournaments.get_absolute_url())
else:
print('НЕВЕРНАЯ ФОРМА')
context = {
'form': form,
}
return render(request, 'tournaments/tournaments_add.html', context)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question