Django form as_table(), as_p() methods not working

10,839

Solution 1

You seem to be passing the form class itself, rather than an instance of it, to the template. You should be doing something like this:

form = LaunchForm()
return render(request, 'my_template.html', {'form': form})

etc.

Edit

To add a field dynamically inside __init__, you need to assign it to the self.fields dictionary:

self.fields['file_name'] = forms.FileField(...)

Solution 2

When there is a FileField, you need to add to the form the enctype in order to send files :

<form action="" method="post" enctype="multipart/form-data">
  {% csrf_token %}
    {{ form.file_name }}
    <table>
        {{ form.as_table }}
    </table>
    <input type="submit" value="Submit">
</form>

See the documentation

Share:
10,839
Steve Sawyer
Author by

Steve Sawyer

Programming for too long to even try to count. Don't like the software business, and believe in doing things right as close to 100% of the time as possible, so I don't usually get along with other programmers (co-workers) very well. I also enjoy biking, playing guitar and woodworking.

Updated on June 13, 2022

Comments

  • Steve Sawyer
    Steve Sawyer almost 2 years

    I have a simple form class with the following single field defined:

    class LaunchForm(forms.Form):
    
        file_name = forms.FileField(required=True, label='File to import')
    

    in my template I have:

    <form action="" method="post">
        {{ form.file_name }}
        <table>
            {{ form.as_table }}
        </table>
        {% csrf_token %}
        <input type="submit" value="Submit">
    </form>
    

    The form is being invoked from the view as follows:

    class Application_Launch_View(View):
    
        form_class = LaunchForm
        template_name = 'application_launch.html'
    
        def get(self, request, app_id):
            form = self.form_class(application_id=app_id)
            return render(request, self.template_name, {'form': form})
    

    I put the variable {{ form.file_name }} into the template just to verify that the form field exists. What is appearing on the page in my browser (along with all the other stuff in the template) is:

    <django.forms.fields.FileField object at 0x032A08F0> 
    

    But the form.as_table doesn't seem to be yielding anything.

    I tried issuing the following function calls immediately after instantiating the form in my view, just before invoking the render() function:

    print(form.file_name) print(form.as_table())

    The server log shows:

    <django.forms.fields.FileField object at 0x032A08F0>
    

    Followed by a blank space. If I remove the call to as_table(), the blank space does NOT appear. I tried substituting form.as_p() in the view, but get the same results, so it appears both methods are at least outputting a CR or CR/FL pair.

    It looks like everything is working except the as_table() and as_p() methods, and I can't figure out why...

    +++++++++++++++++++

    In looking into this a little deeper, I noticed that another form, when referencing a Django field object on the form from within the template doesn't yield the object class description as I posted above, but the expected form field.

    So I have to ask, what is the difference between the two methods below for establishing a form field, and is there a way of making the second method (within the __init__()) work?

    Method 1 (this works):

    class LaunchForm:
    
        file_name = forms.FileField(required=True,
                            label='File to import',
                            initial='Default value')
    

    Method 2 (this isn't working):

    class LaunchForm:
    
        def __init__(self, *args, **kwargs)
    
            self.file_name = forms.FileField(required=True,
                            label='File to import',
                            initial='Default value')
    
  • Steve Sawyer
    Steve Sawyer about 10 years
    I probably should have included my view code - sorry. Edited to add that. Your suggestion did make me go back and verify, but yes, I'm passing an instance, not the class.
  • Steve Sawyer
    Steve Sawyer about 10 years
    You gave me a clue as to what might be wrong here, @DanielRoseman. See my addition above. –