form 也没什么可说的,我只给一个例子大家就懂了
form model
from django import forms class UserForm(forms.Form): username = forms.CharField(label=‘UserName‘, max_length=100) password = forms.CharField(label=‘Password‘, max_length=20, widget=forms.PasswordInput())
views.py
def login(request): if request.method == ‘POST‘: form = UserForm(request.POST) print form.data[‘username‘] print form.data[‘password‘] if form.is_valid(): return HttpResponse(content=‘submit ok‘) return render(request, ‘polls/name.html‘, {‘form‘: form}) else: form = UserForm() return render(request, ‘polls/name.html‘, {‘form‘: form})
模板
<form action="/polls/login/" method="post"> {% csrf_token %} {{ form }} <input type="submit" value="Submit"/> </form>
时间: 2024-10-07 00:06:12