Django populate formset data when editing form - django

I have two models Chapter and ChapterQuestion.
I'm using formset to create multiple ChapterQuestion records while creating Chapter record and it's working fine.
But, when I edit the form, it does not populate formset values.
I'm using UpdateView to update the record
class EditChapter(UpdateView):
model = Chapter
form_class = ChapterForm
template_name = 'courses/chapter/edit_chapter.html'
def get_context_data(self, **kwargs):
context = super(EditChapter, self).get_context_data(**kwargs)
course = Course.objects.get(pk=self.kwargs['course_id'])
if course is None:
messages.error(self.request, 'Course not found')
return reverse('course:list')
context['course'] = course
if self.request.POST:
context['chapter_questions'] = ChapterQuestionFormSet(self.request.POST)
else:
context['chapter_questions'] = ChapterQuestionFormSet()
return context
def form_valid(self, form):
context = self.get_context_data()
chapter_questions = context['chapter_questions']
with transaction.atomic():
self.object = form.save()
if chapter_questions.is_valid():
chapter_questions.instance = self.object
# chapter_questions.instance.created_by = self.request.user
chapter_questions.save()
return super(EditChapter, self).form_valid(form)
def get_success_url(self):
return reverse('course:detail', kwargs={'pk': self.kwargs['course_id']})
#method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(self.__class__, self).dispatch(request, *args, **kwargs)
The urls.py contains
path('<course_id>/chapter/<uuid:pk>/edit', EditChapter.as_view(), name='edit_chapter'),
and in template, I'm using crispy form
<form method="POST" role="form" class="form">
{% csrf_token %}
<h3 class="panel-title">Chapter Detail</h3>
<label for="chapter-name">Chapter Name</label>
<input name="name"
placeholder="Chapter Name"
value="{{ chapter.name }}"
id="chapter-name">
<h3 class="panel-title">Add Question to Chapter</h3>
{{ chapter_questions|crispy }}
</form>
{{ chapter_questions|crispy }} renders the form fields but fields are empty.
forms.py
from django.forms import ModelForm, inlineformset_factory
from courses.models import Chapter, ChapterQuestion
class ChapterForm(ModelForm):
class Meta:
model = Chapter
fields = ['name']
class ChapterQuestionForm(ModelForm):
class Meta:
model = ChapterQuestion
fields = ['word', 'definition']
ChapterQuestionFormSet = inlineformset_factory(Chapter, ChapterQuestion,
form=ChapterQuestionForm, extra=2)
This renders 2 empty set of fields for ChapterQuestion.
How to populated already saved data with formset?

You need to pass instance as shown here:
https://docs.djangoproject.com/en/1.11/topics/forms/modelforms/#inline-formsets
In your case:
context['chapter_questions'] = ChapterQuestionFormSet(instance=self.object)

Related

Submitting a form inside a detail view creates a new object instead of replacing it

I’m trying to create a dashboard for the staff users to fill in and edit some information regarding their users. The form works and saves successfully, but when I submit it for a second time, it creates a new object. It won’t replace the previous:
This is my views.py file:
class ScientificInfoView(FormMixin, DetailView):
model = ScientificInfo
template_name = 'reg/scientific-info.html'
form_class = ScientificInfoForm
def get_success_url(self):
return reverse('scientific-info', kwargs={'pk': self.object.pk})
def get_context_date(self, **kwargs):
context = super(ScientificInfoView, self).get_context_data(**kwargs)
context['form'] = ScientificInfoForm()
return context
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
form.save()
return super(ScientificInfoView, self).form_valid(form)
And my template:
<form method="POST" enctype="multipart/form-data" action="{% url 'scientific-info' pk=object.id %}">
{% csrf_token %}
{{form}}
<button type="submit">submit</button>
</form>
File urls.py:
path('surveys/scientific/<pk>', login_required(views.ScientificInfoView.as_view()), name='scientific-info')
I’m pretty sure that the action part in my form is causing the issue, but how can I solve it?
Use this:
def get_success_url(self):
pk = self.kwargs["pk"]
return reverse("scientific-info", kwargs={"pk": pk})
Or
class ScientificInfoView(FormMixin, DetailView):
model = ScientificInfo
template_name = 'reg/scientific-info.html'
form_class = ScientificInfoForm
def get_success_url(self):
return reverse("scientific-info", args=[pk]) # You can replace pk

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 CreateView without template rendering

I have a problem with saving a form in my CreateView, I found
this solution and it worked for me:
class ScheduleDocumentView(CreateView):
def post(self, request, pk, *args, **kwargs):
form = ScheduleDocumentForm(request.POST, request.FILES)
if form.is_valid():
form.instance.relates_to = Schedule.objects.get(pk=pk)
form.save()
return redirect('planning:schedule-detail', pk=pk)
However my goal is to save a form using form_valid() and get_success_url() without a template in CreateView. I tried something like this(doesn't work):
class ScheduleDocumentView(CreateView):
model = ScheduleDocument
form_class = ScheduleDocumentForm
def form_valid(self, form):
form.instance.relates_to = Schedule.objects.get(pk=pk)
form.save()
return redirect('planning:schedule-detail', pk=pk)
def get_success_url(self):
return reverse('planning:schedule-detail', kwargs={'pk': pk})
It requires a template, is there any other way to handle my post request in DetailView, process it in separate CreateView and redirect it to my DetailView page?
Here's my template for DetailView:
<form enctype="multipart/form-data" action="{% url 'planning:upload-document' schedule.pk %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="button button--secondary">Attach</button>
Urls:
path('schedules/<int:pk>/', ScheduleDetailView.as_view(), name='schedule-detail'),
path('schedules/<int:pk>/upload-document/', ScheduleDocumentView.as_view(), name='upload-document'),
I came across this solution:
class ScheduleDocumentView(CreateView):
model = ScheduleDocument
form_class = ScheduleDocumentForm
def form_valid(self, form):
form.instance.schedule = Schedule.objects.get(pk=self.kwargs['pk'])
return super().form_valid(form)
def get_success_url(self):
return reverse('planning:schedule-detail', kwargs={'pk': self.kwargs['pk']})
template_name is required Django Docs:
The full name of a template to use as defined by a string. Not
defining a template_name will raise a
django.core.exceptions.ImproperlyConfigured exception.
Or in your case Django would cause the default template_name to be 'yourapp/scheduledocument_create_form.html'.
Therefore you get the error TemplateDoesNotExist.
You can get the pk value from self.kwargs(Django Docs).
You can simple create the blank.html template.
class ScheduleDocumentView(CreateView):
http_method_names = ['post']
template_name = 'blank.html' # or use this 'schedule_detail.html'
model = ScheduleDocument
form_class = ScheduleDocumentForm
def form_valid(self, form):
form.instance.relates_to = Schedule.objects.get(pk=self.kwargs.get("pk"))
return super().form_valid(form)
def get_success_url(self):
return reverse('planning:schedule-detail', kwargs={'pk': self.kwargs.get("pk")})
Or use A simple view:
def create_schedule_document(request, pk):
if request.method == 'POST':
form = ScheduleDocumentForm(request.POST, request.FILES)
if form.is_valid():
obj = form.save(commit=False)
obj.relates_to = Schedule.objects.get(pk=pk)
obj.save()
else:
form = ApplyAnonymousForm()
return redirect('planning:schedule-detail', pk=pk)

AttributeError at /home/: 'ActionCodeForm' object has no attribute 'is_bound'

I'm creating a home page for my Django webapp, outside of Django admin. I'd like the home page to have a very simple ModelForm that when submitted, writes to the database.
I'm getting the following error at /home/ currently and not sure how to resolve it.
AttributeError at /home/ 'ActionCodeForm' object has no attribute
'is_bound'
I know about bound and unbound forms and have read the docs, but I am not sure how to actually implement them.
Here is my model:
class ActionCode(models.Model):
action_code = models.CharField(blank=False, max_length=10,
verbose_name="Action Code")
Here is my ModelForm:
class ActionCodeForm(ModelForm):
class Meta:
model = ActionCode
fields = ('action_code',)
def __init__(self, *args, **kwargs):
super(ActionCodeForm).__init__(*args, **kwargs)
Here is my view:
def action_code_form(request):
if request.method == 'GET':
form = ActionCodeForm()
else:
form = ActionCodeForm(request.POST)
if form.is_valid():
action_code = form.cleaned_data['action_code']
form.save()
else:
form = ActionCodeForm()
return render('action_code_form.html', {'form': form},
context_instance=RequestContext(request))
And here is my template, action_code_form.html:
<form method="post" action="">
{% csrf_token %}
<table>
{{ form }}
</table>
<input type="submit" value="Submit"/>
</form>
And urls.py:
from home.views import action_code_form
urlpatterns = [
url(r'^home/', action_code_form, name="home"),
]
You need to check if the form is valid only if the method is POST. Also, the first param of render() must be request
Your view should be as follows:
def action_code_form(request):
form = ActionCodeForm()
if request.method == 'POST':
form = ActionCodeForm(request.POST)
if form.is_valid():
action_code = form.cleaned_data['action_code']
form.save()
return render(request, 'action_code_form.html', {'form': form})
If you need to override __init__() in your model form, then you should add self as a second parameter to super() (there is no need to keep the following two lines if you do not have any specific behavior that you want to add to your form):
def __init__(self, *args, **kwargs):
super(ActionCodeForm, self).__init__(*args, **kwargs)

Django class based view and comments

I'm having trouble with adding comments to class based views,
forms.py:
class RequestForm(ModelForm):
class Meta:
model = Request
exclude = ('slug',)
class RequestCommentForm(ModelForm):
class Meta:
model = RequestComment
fields = ['body' ]
models.py:
class Request(models.Model):
title = models.CharField(max_length=250 )
date = models.DateTimeField('Request date', default=timezone.now, editable=False )
department = models.CharField(max_length=2, choices=DEPARTMENT)
support_request = models.TextField('Request', max_length=2500, blank=True)
owner = models.ForeignKey(User,)
slug = models.SlugField(blank=True, editable=False)
def __unicode__(self):
return self.title
views.py:
class RequestDetailView(ModelFormMixin, DetailView):
model = Request
form_class = RequestCommentForm
def get_success_url(self):
return reverse('request-detail', kwargs={'pk': self.object.pk})
def get_context_data(self, **kwargs):
context = super(RequestDetailView, self).get_context_data(**kwargs)
context['form'] = self.get_form()
return context
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
form.instance.author = self.request.user
form.instance.request = Request.objects.get(pk=self.object.pk)
form.instance.created = timezone.now
form.save()
Also tried this part with:
self.object = form.save(commit=False)
self.object.author = self.request.user
self.object.request = Request.objects.get(pk=self.object.pk)
self.object.created = timezone.now
self.object.save()
return super(RequestDetailView, self).form_valid(form)
template: request_detail.html comments section .....
{% load bootstrap %}
<form action="{% url 'request-detail' object.id %}" method="post"> {% csrf_token %}
<ul class="form-group">
{{ form|bootstrap }}
</ul>
<input class="btn btn-primary" type="submit" value="Submit" />
</form>
.....
Page renders correctly, but when I submit, no-go with saving comment.
Debug toolbar shows that sql queries are updating the request model, instead of request comment.
Can't figure out how to add simple comment form from different model to detail page.
Any help would be appreciated.
Also, if there is more elegant way off adding comments form to class based view, would love to see it. My google-fu didn't help me to find anything.
I know I'm answering this too late, but maybe someone with the same question, in the future, may benefit.
Class RequestDetailView(DetailView):
model = Request
template_name = 'detail.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context ['comment'] = RequestComment.objects. all()
context['form'] = RequestCommentForm()
return context
Class CommentCreateView(CreateView):
model = RequestComment
form_class = RequestCommentForm
def get_success_URL(self):
return reverse ('request: detail', kwargs = {'slug':self.object.post.slug})
def form_valid(self, form):
post = get_object_or_404(Request, slug = self.kwargs ['slug'])
Form.instance.post = Request
return super().form_valid(form)
The URL
path('<slug:slug>/add_comment/', CommentCreateView.as_view(), name = add_comment')
And finally the HTML
<form action="{% url 'request:add_comment' request.slug %}" enctype = "multiparty/form-data" method = "post">
</form>
And that's it. I answered using my phone so there's some typos etc. This is my first answer here, I searched stack overflow for answers I'll post links later. I assumed RequestComment foreignkey field is post and creating models with name 'request' is not encouraged in Django.
Wish I could find a way to do this with CBV, but nope.. function view works great...
class RequestDetailView(ModelFormMixin, DetailView):
model = Request
form_class = RequestCommentForm
def get_success_url(self):
return reverse('request-detail', kwargs={'pk': self.object.pk})
def get_context_data(self, **kwargs):
context = super(RequestDetailView, self).get_context_data(**kwargs)
context['form'] = self.get_form()
return context
def comments(self):
return RequestComment.objects.filter(request=Request.objects.get(pk=self.object.pk))
def RequestCommentAddView(request, pk):
form = RequestCommentForm(request.POST or None)
if form.is_valid() and pk:
form.instance.author = request.user
form.instance.request = Request.objects.get(pk=pk)
form.save()
return HttpResponseRedirect(reverse('request-detail', kwargs={'pk': pk}))
return HttpResponseRedirect(reverse('request-detail', kwargs={'pk': pk}))