from django.contrib import messages
from django.shortcuts import render, redirect
from .forms import BandForm, RepetitionForm
from .models import Band, Repetition, User
def band_form(request):
if request.method == 'POST':
user = User()
band = Band()
rep = Repetition()
_band_form = BandForm(request.POST)
_repetition_form = RepetitionForm(request.POST)
if _band_form.is_valid() and _repetition_form.is_valid():
data = _band_form.cleaned_data
data_rep = _repetition_form.cleaned_data
band_name = data['band_name']
band.band_name = band_name
password = data['password']
idi = user_reg(band_name, password, user)
band.user = idi
band.phone = data['phone']
band.save()
rep.band = band
rep.start_time = data_rep['start_time']
rep.end_time = data_rep['end_time']
rep.date = data_rep['date']
rep.save()
return redirect('/')
else:
print("Форма заполнена не корректно")
else:
_band_form = BandForm()
_repetition_form = RepetitionForm()
return render(request, 'auth.html', {'band_form': _band_form, 'rep_form': _repetition_form})
def user_reg(username, password, user):
user.username = username
user.set_password(password)
user.save()
return user
from django.contrib import messages
from django.shortcuts import render, redirect
from .forms import BandForm, RepetitionForm
from .models import Band, Repetition, User
def band_form(request):
if request.method == 'POST':
user = User()
band = Band()
_band_form = BandForm(request.POST)
# _repetition_form = RepetitionForm(request.POST)
if _band_form.is_valid():
data = _band_form.cleaned_data
band_name = data['band_name']
password = data['password']
idi = user_reg(band_name, password, user)
band.user = idi
band.phone = data['phone']
band.save()
return redirect('/')
else:
messages.error(request, 'Whoo!')
else:
_band_form = BandForm()
_repetition_form = RepetitionForm()
return render(request, 'auth.html', {'band_form': _band_form})
def user_reg(username, password, user):
user.username = username
user.set_password(password)
user.save()
return user
В мета у формы BandForm для fields не указано поле user, значит сохраняться оно не будет даже если передать такой параметрТак поле уникальное же. Если его указать в fields оно рендерится и не даёт вообще ничего создать. После того как я убрал создание экземпляра band.
from django.contrib import messages
from django.shortcuts import render, redirect
from .forms import BandForm, RepetitionForm
from .models import Band, Repetition, User
def band_form(request):
if request.method == 'POST':
_band_form = BandForm(request.POST)
_repetition_form = RepetitionForm(request.POST)
if _band_form.is_valid():
_band_form.save()
return redirect('/')
else:
messages.error(request, 'Whoo!')
else:
_band_form = BandForm()
_repetition_form = RepetitionForm()
return render(request, 'auth.html', {'band_form': _band_form, 'repetition_form': _repetition_form})
from django import forms
from .models import User, Repetition, Band
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('username', 'password')
class BandForm(forms.ModelForm):
class Meta:
model = Band
fields = ('band_name', 'phone', 'vk_page', 'user')
password = forms.CharField(widget=forms.PasswordInput, label="Пароль")
class RepetitionForm(forms.ModelForm):
class Meta:
model = Repetition
fields = ('start_time', 'end_time', 'date')
views.py
forms.py
Не работает. Что я не так делаю?