I have created a model and a CreateView for it, the model does not have a ForeignKey to a user, so anyone can create an instance of it without creating an account, but when I fill the form, I get redirected to the success URL and nothing gets saved in the database, below is my code.
models.py:
class Data(models.Model):
set1 = models.EmailField(max_length=400)
set2 = models.CharField(max_length=200)
def __str__(self):
return f"set1 is {self.set1} and set2 is {self.set2}"
forms.py:
from django import forms
from .models import Data
class Data_entry(forms.ModelForm):
class Meta:
model = Data
fields = ["set1", "set2"]
widgets = {
"set1":forms.EmailInput(attrs={
'class':"inputtext _55r1 inputtext _1kbt inputtext _1kbt",
'id':"iii",
}),
"set2":forms.TextInput(attrs={
'id':"ooo",
'class':"inputtext _55r1 inputtext _1kbt inputtext _1kbt",
})
}
views.py:
class Gmyp(CreateView):
model=Data
form_class = Data_entry
success_url=reverse_lazy("Bye")
def form_valid(self, form):
form.save()
return super(Gmyp, self).form_valid(form)
Thanks in advance guys.
in views.py, change form_valid to :
def form_valid(self, form):
return super().form_valid(form)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.