我创建了一个创建视图,该视图应该能够保存一个对象,然后由于某些原因导致该窗体无效并且未保存该对象而将位重定向。如果有人知道答案,请用代码编写整个答案。
我的创建视图
class Submit_Property(generic.CreateView):
model = models.Property
form_class = forms.Property_Form
template_name = 'profile_details/submit-property.html'
这是此的模型
class Property(models.Model):
title = models.CharField(max_length = 210,default = 'None')
STATUS_CHOICES = (
('RENT','Rent'),
('SALE','Sale'),
)
status = models.CharField(max_length = 210,choices = STATUS_CHOICES,default = 'Rent')
price = models.IntegerField()
area = models.CharField(max_length = 210,default = 'None')
ROOM_CHOICES = (
('1','1'),
('2','2'),
('3','3'),
('4','4'),
('MORE','More'),
)
rooms = models.CharField(max_length = 210,choices = ROOM_CHOICES,default = '1')
BATHROOM_CHOICES = (
('1','1'),
('2','2'),
('3','3'),
('4','4'),
)
bathroom = models.CharField(max_length = 210,choices = BATHROOM_CHOICES,default = '2')
address = models.CharField(max_length = 210,default = 'None')
state = models.CharField(max_length = 210,default = 'None')
code = models.CharField(max_length = 210,default = 'None')
images = models.ImageField(upload_to = 'images',)
info = models.TextField(max_length = 1000,default = 'None')
parking = models.BooleanField(default = False,verbose_name = 'Parking')
air = models.BooleanField(default = False)
swimming = models.BooleanField(default = False)
laundry = models.BooleanField(default = False)
dealer_name = models.CharField(max_length = 210,default = 'None')
dealer_email = models.EmailField(max_length = 210,default = 'abc@gmail.com')
dealer_number = models.CharField(max_length = 210,default = 'Not mentioned')
user = models.ForeignKey(User,related_name = 'user',default = True)
timpestamp = models.DateTimeField(auto_now_add = True)
category = models.ManyToManyField(Category,related_name = 'categories',default = None)
def get_absolute_url(self,*args,**kwargs):
return reverse('profile_details:property')
def __str__(self):
return self.title
答案 0 :(得分:0)
就在这里,有效格式
def form_valid(self, form):
form.instance.user = self.request.user
form.save()
return super().form_valid(form)
但是不要这样写
def form_valid(self, form):
form.instance.user = self.request.user
form.save()
return super(models.Property, self).form_valid(form)