1

So the code I have:

models.py

class Department(models.Model):
    code = models.CharField(max_length=250, unique=True)
    code_meaning = models.CharField(max_length=250, )

    def __str__(self):
        return "%s" % (self.code)

urls.py

path('departments/', DepartmentCreateView.as_view(), name='department'),

department.html

{% extends 'base.html' %} {% block content %}
{% load crispy_forms_tags %}
<div class="content-section">
       
{{ formset.management_form }}
<form method= "POST">
{% csrf_token %}
{% for form in formset %}
 {{ form}}<br><br>
{% endfor %}
<button class="btn btn-outline-info" type="submit">Save</button>
  </form>
</div>
{% endblock content%}

views.py

class DepartmentCreateView(CreateView):
    model =  Department
    form_class = DepartmentForm
    template_name = 'department.html'


    def get_context_data(self, **kwargs):
        context = {'formset': DepartmentFormset}
        return context

   

    def form_valid(self, formset):
        formset.save()

        messages.success(self.request,'Changes were saved.')
        return HttpResponseRedirect(self.get_success_url())
        

    def get_success_url(self):
        return HttpResponseRedirect(reverse('home'))

forms.py

class DepartmentForm(ModelForm):
    class Meta:
      model = Department
      fields = ('code', 'code_meaning',)
DepartmentFormset= modelformset_factory(Department, fields=('__all__'))

My page shows the formset, but when I click on the submit button it just reloads, don't saves anything and the succes message is also not showed on my home page. (My other createviews where I have only one form works prefect)

I very new to django so I don't know where I did something wrong ? Thanks in advance

VdGR
  • 77
  • 7
  • Does this answer your question? [django class-based views with inline model-form or formset](https://stackoverflow.com/questions/4497684/django-class-based-views-with-inline-model-form-or-formset) – Brian Destura Aug 13 '21 at 09:18

1 Answers1

0

VdGR you are trying to use DepartmentFormset in your views but there is no DepartmentFormset in your forms.py. You should make DepartmentFormset in your forms.py then call it in your views. I correct your code and remove DepartmentFormset as you don't have any DepartmentFormset.

class DepartmentCreateView(CreateView):
    model =  Department
    form_class = DepartmentForm
    template_name = 'department.html
  
    def get_context_data(self, **kwargs):
    context[''messages] = messages.success(self.request,'Changes were saved.')
    return context


    def get_success_url(self):
        return HttpResponseRedirect(reverse('home'))

you are doing mistake in your context. Context should be call like this:

 def get_context_data(self, **kwargs):
    context['formset'] =  DepartmentFormset(self.request.POST or None)
    return context

then use formset in your template for calling DepartmentFormset froms.

boyenec
  • 1,405
  • 5
  • 29
  • VdGR I am happy hear that help you. please don't forget to accept the answare. – boyenec Aug 13 '21 at 09:28
  • I'm testing your answer atm but why should I do context['formset'], bcs context = {'formset': DepartmentFormset} works to ? Is better to do it the way you say it ? – VdGR Aug 13 '21 at 10:08
  • VdGR see the doc of django they are using above context for pass data to html template in class based view. if context = {'formset': DepartmentFormset} works then you can use. – boyenec Aug 13 '21 at 10:12
  • boyenec, the problem you suggest is not that there is not a formset, because otherwise I wouldn't see the formset when I open the page (I think), the problem is that when pressing the submit button the page just reloads and doesn't save the changes – VdGR Aug 13 '21 at 10:25
  • Did you using `formset.save()`???if yes then use `form.save()` – boyenec Aug 13 '21 at 10:28
  • I need to be see your views which you are trying now. You are doing mistake somewhere in your views that's the reason data not saving – boyenec Aug 13 '21 at 10:29
  • yes `formset.save()` and also tried `if formset.is_valid(): formset.save()` – VdGR Aug 13 '21 at 10:31
  • That's the problem. Use form.save.() and if form.is_valid() – boyenec Aug 13 '21 at 10:32
  • Also remove fromset inside your function parameters. Just use `def form_valid(self):` – boyenec Aug 13 '21 at 10:36
  • `def form_valid(self): if form.is_valid(): form.save()` like so ? but than is the form not defined ? – VdGR Aug 13 '21 at 10:37
  • Just use this and try and let me know the result. – boyenec Aug 13 '21 at 10:38
  • yeah also not working, but is my get_context_data() correct ? – VdGR Aug 13 '21 at 11:02
  • Copy my whole views.py code and past it in your views. My code should be work. There is no error in code. You are doing mistake in your views that the reason forms not rendering in your html template – boyenec Aug 13 '21 at 11:10
  • Also remove the for loop in your html template – boyenec Aug 13 '21 at 11:11
  • yeah the problem is not that the forms are not rendering, they just don't do anything when you click the submit button – VdGR Aug 13 '21 at 12:32
  • Did you remove for loop in your template? – boyenec Aug 13 '21 at 14:28