[Solved]-Django Form Submit Button

11👍

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

6👍

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})
👤xjcl

Leave a comment