外部キーでフォームを送信できません。外部キーで何かありますか?フォームが保存されていないため、常に destination_list にリダイレクトします。エラーは表示されません。
models.py
class Region(models.Model):
region = models.CharField("Region",max_length=45,blank=True)
status = models.CharField("Status",max_length=45,blank=True)
selected_region = models.CharField("Selected Region",max_length=45,blank=True)
def __str__(self):
return self.region
def get_absolute_url(self):
return reverse('update2', args=[str(self.id)])
def get_delete_url(self):
return reverse('delete2', args=[str(self.id)])
class Destination(models.Model):
region1 = models.ForeignKey(Region)
destination = models.CharField("destination",max_length=30,blank=False,unique=True)
status = models.CharField("status",max_length=30,blank=False,unique=True)
selected_destination = models.CharField("select destination",max_length=30)
def __str__(self):
return self.destination
フォーム.py
class DestinationForm(forms.ModelForm):
class Meta:
model = Destination
fields = [ 'region1','destination','status','selected_destination']
def __init__(self, *args, **kwargs):
super(DestinationForm, self).__init__(*args, **kwargs)
for key in self.fields:
self.fields[key].widget.attrs['class'] = 'form-control'
views.py (フォームが送信されていない場合、destination_list にリダイレクトされます)
def add_destination(request):
form = DestinationForm()
context = {
"form": form
}
if request.method == 'POST':
form = DestinationForm(request.POST)
if form.is_valid():
form.save()
else:
return redirect(destination_list)
return render(request,'adddestination.html',context)