Not able to submit django form - django

I don't know why my form is invalid. Despite everything seems to be normal. I am not getting any error. sending you screenshot of models.py
Here is my forms.py
class LeadForm(forms.ModelForm):
product_queryset = []
product_name = forms.MultipleChoiceField(choices=product_queryset, required=False)
contact = PhoneNumberField()
def __init__(self, *args, **kwargs):
super(LeadForm, self).__init__(*args, **kwargs)
self.fields["product_name"].choices = [(pro.get('id'), pro.get('name')) for pro in
Products.objects.all().values('id', 'name')]
class Meta:
model = Lead
exclude = ('created_at','created_by')
widgets = {
'date_of_enquiry': DateInput(attrs={'type': 'date'})
}
Here is my views.py
def create_lead(request):
u = User.objects.get(username=request.user.get_username())
if request.method == 'POST':
print(request.POST)
# form = LeadForm()
form = LeadForm(request.POST)
if form.is_valid():
print('inside valid')
form.save(commit=True)
form.created_by = u
form.save()
form = LeadForm()
context = {"form": form}
return render(request, 'create_lead.html', context)

You should render the errors you are getting, to do that you need to send to template:
def create_lead(request):
form = LeadForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
instance = form.save(commit=False)
instance.created_by = request.user
instance.save()
return redirect('/sucess_url')
context = {"form": form}
return render(request, 'create_lead.html', context)
And render the errors in template:
{% for field in form %}
{{ field.errors }}
{% endfor %}
More information can be found in documentation regarding form error rendering.
Probably you should remove the __init__ method from Form class, instead add a __str__ method in Product model:
class Product(models.Model):
# rest of the code
def __str__(self):
return self.name

Please do in your forms.py
class Meta:
model = Lead
exclude = ('created_at','created_by')
fields = ('product_name', [and all your fields which you want to include])
widgets = {
'date_of_enquiry': DateInput(attrs={'type': 'date'})
}

Related

How to point django form to pk?

I am trying to make a form that is dynamically selected by a DetailView object.
I want to click the DetailView link and be taken to a form whose primary key is the same as the primary key from my detail view. When I attempt to do this I get an error. How can I do this? Is their a prebuilt library that will assist me?
My Model:
'''
class MemberStudent(models.Model):
instructor = models.ForeignKey(Teachers, on_delete=models.CASCADE)
name = models.CharField(max_length=50, default="Doe")
age = models.IntegerField(default=99)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('student_detail', args=[str(self.id)])
class BehaviorGrade(models.Model):
SEVERITY = [
('Bad','Bad Behavior'),
('Good','Good Behavior'),
('Danger','Dangerous'),
]
LETTER_GRADE = [
('A','A'),
('B','B'),
('F','F'),
]
studentName = models.ForeignKey(MemberStudent, on_delete=models.CASCADE) #need a link to student name
eventGrade = models.CharField(max_length=1, choices=LETTER_GRADE)
eventSeverity = models.CharField(max_length=15, choices=SEVERITY)
eventTitle = models.CharField(max_length=15)
eventDescription = models.TextField()
def __str__(self):
return self.eventTitle
'''
My Views:
'''
class StudentDetailView(FormMixin,DetailView):
model = MemberStudent
template_name = 'student_detail.html'
form_class = BehaviorForm
def get_success_url(self):
return reverse('student_detail', kwargs={'pk':self.object.pk})
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
form.save()
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
return super().form_valid(form)
'''
my forms.py:
'''
class BehaviorForm(ModelForm):
class Meta:
model = BehaviorGrade
fields = ['eventGrade',
'eventSeverity','eventTitle',
'eventDescription']
'''
my url:
'''
path('students/<int:pk>', StudentDetailView.as_view(), name='student_detail'),
'''
my htmltemplate(its a detailview with a form below on the same page):
'''
{{ object.id }}
{{ object.name }}
{{ object.age }}
{{ object.instructor }}
<form action="{% url 'student_detail' object.id %}" method="POST">
{{ form }}
{% csrf_token %}
<input type="submit" value="REVIEW">
</form>
'''
error message when form submitted:
"IntegrityError at /student/students/1
NOT NULL constraint failed: Students_behaviorgrade.studentName_id"
The form also is sent without the PK I have requested in my form code.
the django error log shows the PK is never sent
here is the log message on a test of dummy data:
params [None, 'A', 'Bad', 'asdf', 'asdf']
That's because you have to manually set the member student id on the behavior garde.
You can do it like this in the post function of your view :
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
behavior_grade = form.save(commit=False)
behavior_grade.studentName = self.object
return self.form_valid(form)
else:
return self.form_invalid(form)
You can also take a look at this method to use the form_valid function to do it :
## Include the instance object before saving
def form_valid(self, form):
form.instance.studentName = self.object
return super().form_valid(form)
The solution was to to call the primary key in the model method. Then to save to the correct model object I called .pk on my object. I'm cannot elegantly explain this solution however upon testing several different scenarios it works.
'''
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
behavior_grade = form.save(commit=False)
behavior_grade.studentName = self.object #passes unamed form field
behavior_grade.studentName.pk = form.save(commit=True) #calls key
return self.form_valid(form)
else:
return self.form_invalid(form)
'''

Django Form : after correctly submitting form I got this: "this field is required"

I've a category and I've added a form for user in each category.
So I've two fields to fill and after filling them correctly I submit but the page reload, and nothing appears in my DB... only one error on Image field: This field required. I don't really know what's wrong here.
class Picture(models.Model):
catego = models.ForeignKey(Catego,on_delete=models.CASCADE,related_name="catego_pictures")
user = models.ForeignKey(User, blank=True, null=True,on_delete=models.CASCADE,related_name='user_pictures')
image = models.ImageField(upload_to='nutriscore/')
pictureoption = models.CharField(max_length=20,choices=Pictureoption.choices,default=Pictureoption.HOME,)
publishing_date = models.DateField(auto_now_add=True)
class CreatePictureForm(forms.ModelForm):
def __init__(self,*args,**kwargs):
super(CreatePictureForm, self).__init__(*args,**kwargs)
self.helper = FormHelper()
self.helper.form_method="post"
self.helper.layout = Layout(
Field("image",css_class="single-input"),
Field("pictureoption",css_class="single-input"),
)
self.helper.add_input(Submit('submit','Upload a pic',css_class="single-input textinput textInput form-control"))
class Meta:
model = Picture
fields = [
'image',
'pictureoption',
]
def __str__(self):
return self.catego.name
views.py
#login_required(login_url='/cooker/login')
def catego(request, slug):
catego = Catego.objects.get(slug=slug)
context = {
'catego': catego
}
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = CreatePictureForm(request.POST)
# check whether it's valid:
if form.is_valid():
form.instance.catego = self.object
form.instance.user = self.request.user
form.save()
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
else:
form = CreatePictureForm()
context['form'] = form # add `form` to the context
return render(request, 'post_catego.html', context)
here is the answer
#login_required(login_url='/cooker/login')
def catego(request, slug):
catego = Catego.objects.get(slug=slug)
context = {
'catego': catego
}
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = CreatePictureForm(request.POST, request.FILES)
# check whether it's valid:
if form.is_valid():
form.instance.catego = catego
form.instance.user = request.user
form.save()
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
else:
form = CreatePictureForm()
context['form'] = form # add `form` to the context
return render(request, 'post_catego.html', context)

Default the value of an external key in a form

In the form for adding a calendar (of the Model "Calendar"), in the "group" field shows a drop-down menu where all the "CalendarGroups" are. I would like this menu not to be there and "group" to be set by default with the "group" id which I pass through the url parameter ("group_id"). How could I solve this?
In models.py:
class CalendarGroups(models.Model):
name = models.CharField(max_length = 155, blank=True, null=True, unique=True)
def __str__(self):
return str(self.name)
#property
def get_html_url(self):
url = reverse('', args=(self.id,))
return f' {self.name} '
class Calendar(models.Model):
name = models.CharField(max_length=200, unique=True)
#created_by
group = models.ForeignKey(CalendarGroups, on_delete = models.CASCADE, default='')
#property
def get_html_url(self):
url = reverse('cal:calendar_view', args=(self.id, self.group))
return f' {self.name} '
In forms.py:
class CalendarGroupsForm(ModelForm):
class Meta:
model = CalendarGroups
fields = ('name',)
def __init__(self, *args, **kwargs):
super(CalendarGroupsForm, self).__init__(*args, **kwargs)
class CalendarForm(ModelForm):
class Meta:
model = Calendar
fields = ('name', 'group')
def __init__(self, *args, **kwargs):
super(CalendarForm, self).__init__(*args, **kwargs)
In views.py:
def group(request, group_id=None):
instance = CalendarGroups()
if group_id:
instance = get_object_or_404(CalendarGroups, pk=group_id)
else:
instance = CalendarGroups()
form = CalendarGroupsForm(request.POST or None, instance=instance)
if request.POST and form.is_valid():
form.save()
return HttpResponseRedirect(reverse('cal:home'))
return render(request, 'cal/form.html', {'form': form})
def calendar(request, group_id=None):
instance = Calendar()
form = CalendarForm(request.POST or None, instance=instance)
if request.POST and form.is_valid():
form.save()
return HttpResponseRedirect(reverse('cal:home'))
return render(request, 'cal/form.html', {'form': form})
In urls.py:
url(r'^home/(?P<group_id>\d+)/calendar/new/$', views.calendar, name='calendar_new')
After consulting the documentation, I solved it by modifying the code as follows:
In views.py:
def calendar(request, group_id=None):
instance = Calendar()
instance = Calendar(group_id=group_id)
form = CalendarForm(request.POST or None, instance=instance)
if request.POST and form.is_valid():
form.save()
return HttpResponseRedirect(reverse('cal:home'))
return render(request, 'cal/form.html', {'form': form})
In forms.py:
class CalendarForm(ModelForm):
class Meta:
model = Calendar
fields = ('name',)
def __init__(self, *args, **kwargs):
super(CalendarForm, self).__init__(*args, **kwargs)
You already have send to the views.calendar the group_id key you need.
Now try to add it to the form before the form.save
Try to modify like this in view.py
def calendar(request, group_id):
instance = Calendar()
form = CalendarForm(request.POST or None, instance=instance)
if request.POST and form.is_valid():
form.group=group_id
form.save()
return HttpResponseRedirect(reverse('cal:home'))
return render(request, 'cal/form.html', {'form': form})
Take note that might override what user can post on CalendarForm and you should consider to hide or make not editable that filed group_id,
Also this is slightly different to what i usually do but it should work
I Post also an example of what works for me:
def calendar(request, group_id):
instance = Calendar()
form = CalendarForm(request.POST or None)
if request.POST and form.is_valid():
instance = form.save(commit=False)
instance.group = group_id
instance.save()
return HttpResponseRedirect(reverse('cal:home'))
return render(request, 'cal/form.html', {'form': form})

create help text for a field dynamically

I have my response form and view like this
class ResponseForm(ModelForm):
class Meta:
model = ResponseModel
exclude = ('author', 'title','submit_count')
# help_texts = {
# 'ans1': user.q1.value,
# }
#login_required
def ResponseFormView(request):
if request.method == "POST":
form = ResponseForm(request.POST)
if form.is_valid():
submission = form.save(commit=False)
submission.author = request.user
submission.save()
return render(request, 'thanks.html', {})
else:
form = ResponseForm()
return render(request, 'response_tem.html', {'form': form})
I want the help text for 'ans1' field to be the value of q1 field of request.user. How do I do it?
You can do it like this:
class ResponseForm(ModelForm):
def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None) # popping user from known arguments
super(ResponseForm, self).__init__(*args, **kwargs)
if user:
self.fields['ans1'].help_text = "Help Text for {}".format(user.username)
class Meta:
model = ResponseModel
exclude = ('author', 'title','submit_count')
#login_required
def ResponseFormView(request):
if request.method == "POST":
form = ResponseForm(request.POST)
if form.is_valid():
submission = form.save(commit=False)
submission.author = request.user
submission.save()
return render(request, 'thanks.html', {})
else:
form = ResponseForm(user=request.user) # passing user as known argument
return render(request, 'response_tem.html', {'form': form})
Here, in the view I am passing the request.user as known argument when I am initiating Form Class's Object (marked with comment). Then in the Form, I am catching the user sent from view and updating the field's help text.

How to define initial values for ModelForm rightly?

I have no idea and I need to ask your advice.
I have simple form:
class CommentForm(ModelForm):
class Meta:
model = Comment
fields = ['text','author','news']
I want to add form in DetailView and hadle this form there:
class NewsDetailView(DetailView):
model = News
template_name = 'news/detail.html'
def get_initial(self):
return {'news': self.get_object(), 'author': self.request.user}
def get_context_data(self, **kwargs):
context = super(NewsDetailView, self).get_context_data(**kwargs)
context['form'] = CommentForm(initial=self.get_initial())
return context
def post(self, request, *args, **kwargs):
'''
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
comment_form.save()
I don't want to show 'author' and news fieds. But if I hide them I can't to get initial values..
UPDATED:
After form validation I need return current form instance in the template through updating page. I attempted the next:
comment_form = CommentForm(request.POST, request=request)
if comment_form.is_valid() and comment_form.validate_user():
comment_form.save()
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
else:
context = super(NewsDetailView,self).get_context_data(**kwargs)
context['form'] = comment_form
return self.render_to_response(context)
But did not work.
If you don't render your fields using {{ form.author }} and {{ form.news }} the form won't validate. Try using a HiddenInput for each field, You can do that by overriding the __init__ method of your form:
class CommentForm(ModelForm):
def __init__(self, *args, **kwargs):
super(CommentForm, self).__init__(*args, **kwargs)
self.fields['author'].widget = forms.HiddenInput()
self.fields['news'].widget = forms.HiddenInput()
class Meta:
model = Comment
fields = ['text','author','news']