| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- from django import forms
- from .models import Musician, Song, Rating, Meloman
- from django.contrib.auth.models import User
- from django.contrib.auth.forms import UserCreationForm
- class MusicianForm(forms.ModelForm):
- class Meta:
- model = Musician
- fields = ['name', 'city', 'birth_year']
- widgets = {
- 'name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Название исполнителя'}),
- 'city': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Город'}),
- 'birth_year': forms.NumberInput(attrs={'class': 'form-control', 'placeholder': 'Год основания'}),
- }
- labels = {
- 'name': 'Имя исполнителя',
- 'city': 'Город',
- 'birth_year': 'Год рождения/основания',
- }
- class SongForm(forms.ModelForm):
- class Meta:
- model = Song
- fields = ['title', 'duration_sec', 'album', 'genre']
- widgets = {
- 'title': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Название песни'}),
- 'duration_sec': forms.NumberInput(attrs={'class': 'form-control', 'placeholder': 'Длительность в секундах'}),
- 'album': forms.Select(attrs={'class': 'form-control'}),
- 'genre': forms.Select(attrs={'class': 'form-control'}),
- }
- class RatingForm(forms.ModelForm):
- class Meta:
- model = Rating
- fields = ['score']
- widgets = {
- 'score': forms.NumberInput(attrs={'min': 1, 'max': 10, 'class': 'form-control', 'style': 'width: 100px;'})
- }
-
- class RegistrationForm(UserCreationForm):
- email = forms.EmailField(required=True, widget=forms.EmailInput(attrs={'class': 'form-control'}))
- full_name = forms.CharField(max_length=150, required=True, widget=forms.TextInput(attrs={'class': 'form-control'}))
- city = forms.CharField(max_length=100, required=False, widget=forms.TextInput(attrs={'class': 'form-control'}))
- birth_year = forms.IntegerField(required=False, widget=forms.NumberInput(attrs={'class': 'form-control'}))
- gender = forms.ChoiceField(choices=[('M', 'Мужской'), ('F', 'Женский')], required=False, widget=forms.Select(attrs={'class': 'form-control'}))
-
- class Meta:
- model = User
- fields = ['username', 'email', 'password1', 'password2']
- widgets = {
- 'username': forms.TextInput(attrs={'class': 'form-control'}),
- 'email': forms.EmailInput(attrs={'class': 'form-control'}),
- 'password1': forms.PasswordInput(attrs={'class': 'form-control'}),
- 'password2': forms.PasswordInput(attrs={'class': 'form-control'}),
- }
-
- def save(self, commit=True):
- user = super().save(commit=False)
- user.email = self.cleaned_data['email']
- if commit:
- user.save()
- # Создаем профиль меломана
- Meloman.objects.create(
- user=user,
- full_name=self.cleaned_data['full_name'],
- city=self.cleaned_data.get('city', ''),
- birth_year=self.cleaned_data.get('birth_year'),
- gender=self.cleaned_data.get('gender')
- )
- return user
|