栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

django forms_django modelform?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

django forms_django modelform?

网上教程大多是旧版django,本教程记录我解决form表单的一个问题

一、创建HTML的FORM表单

index.html


二、创建对应的视图

views.py

def search(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        message = request.POST.get('word')

        # create a form instance and populate it with data from the request:
        form = NameForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            return HttpResponse(f'')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = NameForm()

    return render(request, 'index.html', {'form': form})
三、构建一张Form类表单

forms.py

from django import forms

class NameForm(forms.Form):
    word = forms.CharField(label='搜索', max_length=100)
四、urls调度器

urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('search/', views.search,name = 'search')
]
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/783215.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号