美文网首页
django获取用户数据方法及FBV和CBV

django获取用户数据方法及FBV和CBV

作者: xin激流勇进 | 来源:发表于2018-01-16 10:39 被阅读0次

获取用户数据

FBV:function base views
CBV:class base views
定义类时,获得用户请求时,会自动调用父类View的dispath方法。
所以可以让自定义类继承父类dispath方法,这样可以自我定义一些功能。
url.py

from django.contrib import admin
from django.urls import path
from cmdb import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.index),
    path('login/', views.login),
    path('home/', views.Home.as_view()),

]

app下的view.py

from django.shortcuts import render, HttpResponse


# Create your views here.


def index(request):
    return HttpResponse('<h1>index</h1>')


def login(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    elif request.method == 'POST':
        v = request.POST.get('gender')
        print(v)
        user = request.POST.get('user')
        pwd = request.POST.get('pwd')
        print(user, pwd)
        love = request.POST.getlist('love')
        print(love)
        city = request.POST.getlist('city')
        print(city)
        file = request.FILES.get('file')
        print(file, type(file))
        import os
        file_path = os.path.join('upload', file.name)
        f = open(file_path, 'wb')
        # from django.core.files.uploadedfile import InMemoryUploadedFile
        for content in file.chunks():
            f.write(content)

        f.close()

        return render(request, 'login.html')
    else:
        pass


from django.views import View


class Home(View):
    def dispatch(self, request, *args, **kwargs):
        print('before')
        result = super(Home, self).dispatch(request, *args, **kwargs)
        print('after')
        return result

    def get(self, request):
        print('method:', request.method)
        return HttpResponse('<b>ok</b>')

    def post(self, request):
        pass

前端.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="/login/" method="POST" enctype="multipart/form-data">
        <p>
            <input name="user" type="text" placeholder="用户名">
        </p>
        <p>
            <input name="pwd" type="password" placeholder="密码">
        </p>
        <p>
            男:<input type="radio" value="1" name="gender">
            女:<input type="radio" value="0" name="gender">
        </p>
        <p>
            篮球:<input type="checkbox" value="1" name="love">
            足球:<input type="checkbox" value="2" name="love">
            棒球:<input type="checkbox" value="3" name="love">
        </p>
        <p>
            <select name="city" multiple>
                <option value="北京">北京</option>
                <option value="上海">上海</option>
                <option value="杭州">杭州</option>
                <option value="蚌埠">蚌埠</option>
                <option value="淮北">淮北</option>
            </select>
        </p>
        <p>
            <input type="file" name="file">
        </p>
        <p>
            <input type="submit" value="提交">
        </p>
    </form>
</body>
</html>

路由系统

from django.urls import path, re_path
urlpatterns = [
    re_path('index/(\d+)/(\d+)/', views.index, name='index'),
    re_path('detail-(?P<nid>\d+)-(?P<uid>\d+).html/', views.detail, name='detail'),
]

def index(request, *args, **kwargs):
    print(*args, **kwargs)
    print(request.path_info)
    url = reverse('index', args=(1, 2))
    print('url:', url)
    return render(request, 'index.html', {'user_dict': USER_DICT})

<form method="post" action="{% url 'index' 1 2 %}">
        <input type="text" name="user" placeholder="用户">
        <input type="submit" value="提交">
</form>

路由分发

在django中创建多个app时,为了有效的分类url
工程名目录下urls.py

from django.urls import path, re_path, include
urlpatterns = [
    path('cmdb/', include('cmdb.urls')),
]

在app下创建urls.py

from django.conf.urls import url,include
from django.contrib import admin
from app02 import views

urlpatterns = [
                url(r'^login/', views.login),
            ]

相关文章

  • DRF进阶

    一、Django的FBV和CBV FBV:Function-base views基于函数的视图CBV:Class-...

  • django获取用户数据方法及FBV和CBV

    获取用户数据 FBV:function base viewsCBV:class base views定义类时,获得...

  • django中的FBV和CBV

    django中请求处理方式有2种:FBV 和 CBV FBV FBV(function base views) 就...

  • Django使用CBV处理请求

    在Django中有两种基本的处理用户请求的方式,分别是FBV和CBV,这里讲的是关于使用CBV处理用户请求的方式 ...

  • Django-FBV 和 CBV

    FBV 和 CBV django中请求处理方式有2种: FBV(function base views)** 就是...

  • Django中的CBV

    django中请求处理方式有2种:FBV 和 CBV CBV CBV(class base views) 就是在视...

  • Django 的 cbv

    正如我们了解到的,Django 写视图函数有两种写法:cbv 和 fbv。cbv 提倡使用类来写,fbv 使用函数...

  • Django之FBV和CBV(dispatch方法)

    一、FBV与CBV在我们日常学习Django中,都是用的FBV(function base views)方式,就是...

  • django笔记(四)视图

    FBV:基于方法的视图########################################## CBV...

  • Django的CBV和FBV

    django提供了两种对应关系。 views.py中就是使用的FBV。 urls.py中使用: CBV的方式: u...

网友评论

      本文标题:django获取用户数据方法及FBV和CBV

      本文链接:https://www.haomeiwen.com/subject/tszjoxtx.html