Issue
I am trying to save data in database using django form but it not saving it this is my
forms.py
class PlanForm(forms.ModelForm):
class Meta:
model = Plans
fields = ['plan_name', 'plan_note', 'plan_price', 'access_to']
plan_name = forms.CharField(max_length=255, widget=forms.TextInput( attrs={
'class':'form-control',
'id':'plan_name'
} ))
plan_note = forms.CharField(max_length=255, widget=forms.TextInput( attrs={
'class':'form-control',
'id':'plan_note'
} ))
plan_price = forms.CharField(max_length=255, widget=forms.TextInput( attrs={
'class':'form-control',
'id':'plan_price'
} ))
can_access = forms.ModelMultipleChoiceField(
queryset=Add_e_office.objects.all(),
widget=forms.CheckboxSelectMultiple
)
and this my views.py
def add_plan(request):
if request.method == 'POST':
form = PlanForm(request.POST)
if form.is_valid():
form.save(commit=False)
form.user = request.user
form.save()
messages.success(request,"Plan created successfully.")
return redirect(request.path)
return render(request, 'backend/add_plan.html',{'form':form})
else:
form = PlanForm()
return render(request, 'backend/add_plan.html',{'form':form})
when i submit my form i receive POST
request but it did not save data in data base actualy it ignore form.is_valid()
part.
Solution
After looking at this post https://stackoverflow.com/a/11072097/14457833 i got a solution user is not in my form fields and i am trying to access it that's why i am not able to save data in my database. After removing user and keeping only form.is_valid()
is worked.
Answered By - Ankit Tiwari
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.