1

我无法使用外键提交表单。有外键吗?我总是重定向到destination_list,因为表单没有保存。没有错误显示。

模型.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) 
4

0 回答 0