开发者

Django Form Submit Button

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

class UploadFileForm(forms.Form):
    category = forms.ChoiceField(get_category_list())
    file = forms.Fi开发者_StackOverflowleField()

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


<input type="submit" value="Gogogo!" />


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})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜