1. # views.py
    2. from django.http import HttpResponseRedirect
    3. from django.shortcuts import render
    4. from .forms import NameForm
    5. def get_name(request):
    6. # if this is a POST request we need to process the form data
    7. if request.method == 'POST':
    8. # create a form instance and populate it with data from the request:
    9. form = NameForm(request.POST)
    10. # check whether it's valid:
    11. if form.is_valid():
    12. # process the data in form.cleaned_data as required
    13. # ...
    14. # redirect to a new URL:
    15. return HttpResponseRedirect('/thanks/')
    16. # if a GET (or any other method) we'll create a blank form
    17. else:
    18. form = NameForm()
    19. return render(request, 'name.html', {'form': form})
    1. # name.html
    2. <form action="/your-name/" method="post">
    3. {% csrf_token %}
    4. {{ form }}
    5. <input type="submit" value="Submit">
    6. </form>

    更改查询集