11

I have a pretty simple file upload form class in django:

class UploadFileForm(forms.Form):
    category = forms.ChoiceField(get_category_list())
    file = forms.FileField()

one problem is that when i do {{ form.as_p }}, It has no submit button. How do i add one?

2 Answers 2

11
<input type="submit" value="Gogogo!" />
Sign up to request clarification or add additional context in comments.

1 Comment

As humorous as this is, it's not a very complete answer. Put that below {{ form.as_p }} before you close it off with </form>.
7

This is a template for a minimal HTML form (docs):

<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit">
</form>

Put that under <PROJECT>/templates/MyForm.html and then replace 'DIRS': [] in <PROJECT>/<PROJECT>/settings.py with the following:

        'DIRS': [os.path.join(BASE_DIR, "templates")],

Code like this can then be used to serve your form to the user when he does a GET request, and process the form when he POSTs it by clicking on the submit button (docs):

from django.http import HttpResponse, HttpResponseNotFound
from django.shortcuts import render
from . import forms

def my_form(request):
    if request.method == 'POST':
        form = forms.MyForm(request.POST)
        if form.is_valid():
            return HttpResponse('Yay valid')
    else:
        form = forms.MyForm()

    return render(request, 'MyForm.html', {'form': form})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.