2

I am attempting to use a class-based view to display information about a tool, and then add tags to that view, mostly to teach myself how to use inline_formsets. The problem that I am having is how to inject the child object's form into the template.

The problem is that the child's formsets are appearing, but there is no parent form displaying from the template.

The result is that the parent form doesn't show -

enter image description here

Ultimately, this is a "What am I doing wrong in Django?" question.

The model is very simple - Tool has a few fields that define my parent object, and tag is a child that is related to it:

models.py

 from django.db import models

 class Tool(models.Model):
    content_id = models.CharField(
        primary_key=True,
        help_text="Confluence ID of the tool document",
        max_length=12
    )
    
    tool_name = models.CharField(
        max_length=64,
        help_text="Short name by which the tool is called",
    )
    
    purpose = models.TextField(
        help_text="A one sentence summary of the tools reason for use."
    )
    
    body = models.TextField(
        help_text="The full content of the tool page"
    )
    
    last_updated_by = models.CharField(
        max_length=64
    )
    
    last_updated_at = models.DateTimeField()
    
    def __unicode__(self):
        return u"%s content_id( %s )" % (self.tool_name, self.content_id)
    
 class ToolTag(models.Model):

    description = models.CharField(
        max_length=32,
        help_text="A tag describing the category of field.  Multiple tags may describe a given tool.",
    )
    
    tool = models.ForeignKey(Tool)
    
    def __unicode__(self):
        return u"%s describes %s" % (self.description, self.tool)

I am using standard Class-based forms:

forms.py

from django.http import HttpResponseRedirect
from django.views.generic import CreateView
from django.views.generic import DetailView, UpdateView, ListView
from django.shortcuts import render

from .forms import ToolForm, TagsFormSet
from .models import Tool
        
TagsFormSet = inlineformset_factory(Tool, ToolTag, can_delete='True')

class ToolUpdateView(UpdateView):
    template_name = 'tools/tool_update.html'
    model = Tool
    form_class = ToolForm
    success_url = 'inventory/'

views.py

def call_update_view(request, pk):
    form = ToolUpdateView.as_view()(request,pk=pk)
    tag_form = TagsFormSet()
    return render(  request, "tools/tool_update.html", 
                    {
                        'form': form,
                        'tag_form': tag_form,
                        'action': "Create"
                    }
    )
    

And my template is as follows:

tool_update.html

{% block content %}

<form action="/update/" method="post">
    {% csrf_token %}
    
    <DIV>
        Tool Form:
        {{ form.as_p }}
    </DIV>
    
    <DIV>
        Tag Form:
        {{ tag_form.as_p }}
    </DIV>
    <input type="submit" value="Submit" />
</form>

{% endblock %}
Community
  • 1
  • 1
Affable Geek
  • 465
  • 1
  • 9
  • 19

1 Answers1

1

This line:

form = ToolUpdateView.as_view()(request,pk=pk)

makes no sense at all. A view is not a form, and you can't use it as one. You should just do form = ToolForm(). Although note you also need some code to process the form submission itself, from the request.POST data.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Ah, okay - I was understanding the Class-Based Views to be forms that had already done much more of the work. Sounds like my error was in assuming the framework was going to give me that. Thanks - And to be clear, that means I can't lean on the Class-Based Views in this situation at all, and I do need to flesh out my own forms. – Affable Geek Nov 05 '13 at 16:40
  • Actually, now I think about it, you probably want to do this the other way round: use the `ToolUpdateView` as your view, but override `get_context_data()` and `form_valid()` to add and process your formset, in addition to the form. – Daniel Roseman Nov 05 '13 at 16:42
  • Yes, that does sound better - that way, I can lean on the view to do most of my processing, but inject the formset there. Off to the interwebs to find an example! (Unless, if I could impose on you to revise the answer :) Thanks again! – Affable Geek Nov 05 '13 at 16:43
  • http://stackoverflow.com/questions/7911858/django-generic-class-based-view-example-where-does-kwargs-come-from – Affable Geek Nov 05 '13 at 16:45