简体   繁体   English

Django 表单提交按钮

[英]Django Form Submit Button

I have a pretty simple file upload form class in django:我在 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.一个问题是,当我执行{{ form.as_p }} ,它没有提交按钮。 How do i add one?我如何添加一个?

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

This is a template for a minimal HTML form ( docs ):这是一个用于最小 HTML 表单 ( 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:将其放在<PROJECT>/templates/MyForm.html ,然后将<PROJECT>/<PROJECT>/settings.py 'DIRS': [] <PROJECT>/templates/MyForm.html 'DIRS': []替换为以下内容:

        '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 ):然后可以使用这样的代码在用户执行 GET 请求时将您的表单提供给用户,并在他通过单击提交按钮 ( 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})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM