Django实例

下面我们进行一个Django的几个小小的实例:
在前面三篇Django的基础操作中,我们已经建好了基础的文件,我先给大家看一下成型之后的文件夹包含的内容:这里写图片描述
总的文件夹名字是wartercar,然后里面包含一个起始子文件:wartercar子文件,user文件是我们创建的模块文件:程序为 python manage.py startapp user。user名字要在wartercar同名文件下的sitting.py文件中的:INSTALLED_APPS={}路径中添加进去。结果为这里写图片描述
要在templates文件是作者自己去watercar总文件下去创建的。
然后去sitting文件里面修改这里写图片描述l面的数据,内容按照你之前创建的数据库。
之后你可以去改这里写图片描述这两个东西,也可以不改。
这些步骤在前面三篇文档里面都有,我就直接上程序截图了,

models的修改:这里写图片描述

views的修改:

from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render
from user.models import UserAddress,UserInfo,AreasInfo
from django.db.models import Q,Max,F,Sum
# Create your views here.
# def index(request):
#     # return HttpResponse('欢迎')
#     context = {'bianliang':'假设是数据库取出来的数据,我自己瞎写的'}
#     return render(request,'index.html',context=context)
def index(request1):
    # return HttpResponse('欢迎')

    obj = AreasInfo.objects.get(id=2)

    context = {'obj':obj}
    return render(request1,'index.html',context=context)


def user_show(request):
    user_obj = UserInfo.objects.all()
    UserInfo.objects.filter()
    # print(user_obj)
    context = {'user_obj':user_obj}
    return render(request,'show_user.html',context=context)

def register(request):
    return render(request,'register.html')

def delete(request,user_id):
    '''实现删除用户功能'''
    try:
        obj = UserInfo.objects.get(id=user_id)
        obj.delete()
    except Exception as e:
        print('你要删除的用户已经不存在')

    return  HttpResponse('已经删除')
def add_user(request):
    user_new = UserInfo()
    user_new.user_name = '王热狗'
    user_new.password = '114422556633'
    user_new.email = '114477'
    user_new.phone = 115599
    user_new.save()
    return  HttpResponse('增加成功')

def seacher_user(request):
    # obj9 = UserInfo.objects.filter(user_name__contains='王')
    # obj1 = UserInfo.objects.filter(user_name__endswith='王')
    # obj2 = UserInfo.objects.filter(user_name__starswith='王')
    # obj3 = UserInfo.objects.filter(id__gte=5)
    # obj4 = UserInfo.objects.filter(id=5)
    # obj5 = UserInfo.objects.get(id=5)
    # obj6 = UserInfo.objects.filter(id__in=[1, 2, 3])
    # obj7 = UserInfo.objects.filter.xeclude(id=3)
    # obj7 = UserInfo.objects.filter(password='123456', id__gt=3)
    # obj8 = UserInfo.objects.filter(password='123456', id__gt=3).exclude(phone=188)
    # obj = UserInfo.objects.aggregate(Sum('id'))
    # obj = UserInfo.objects.aggregate(Max('id'))
    # UserInfo.objects.count()
    # obj = UserInfo.objects.filter(Q(password='226677')&Q(id__gt=2) &~ Q(phone=75856562))
    # obj = UserInfo.objects.filter(id__gt=F('phone'))
    person = UserInfo.objects.get(id=1)
    obj = person.useraddress_set.all()
    city = UserAddress.objects.get(detail='北京望京')

    print(city.user)

    # obj = UserInfo.objects.filter(id__gt=F('phone'))
    context = {'obj': obj}
    return render(request, 'search_user.html', context=context)

在user文件下创建urls.py文件,里面的程序为:

from django.conf.urls import include, url
from user import views

urlpatterns = [
    # 127.0.0.1/index
    url(r'^index$',views.index),
    url(r'^user$',views.user_show),
    url(r'^register$',views.register),
    url(r'^delete(\d+)$',views.delete_user),
    url(r'^add_user$',views.add_user),
    url(r'^search$',views.search_user),
]

然后在同名文件下的urls.py文件里面添加这么一句话:
url(r’^’,include(‘user.urls’))与user文件中的urls文件关联起来

然后index。html’文件:

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>{{ bianliang }}</h1>
    当前所在的省:{{ obj.parent.name }}
    当前的市:{{ obj.name }}

    当前的区:
{% for i in obj.areasinfo_set.all %}
    {{ i.name }}

{% endfor %}

</body>
</html>

show_user。html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul>
<!--{{ user_obj.user_name }}}-->
    {% for obj in user_obj %}
    <li>{{ obj.user_name }}  {{ obj.id }} -------<a href="delete{{ obj.id }}">删除</a></li>

    {% endfor %}

    <a href="add_user"> 增加</a>
</ul>>
</body>
</html>

search_user。html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    查询所有名字中带王的人
    <ul>
    {% for obj_one in obj %}
        <li>{{ obj_one.user_name }}</li>
        {% endfor %}
    </ul>


</body>
</html>

然后读者测试的时候一定要与之前的虚拟环境结合起来,进入自己创建的虚拟环境中的文件夹中,作者这里是wartercar,然后打出:python manage.py runserver(默认是8000端口)+端口这里写图片描述 鼠标右键点击红色处。下面展示几个结果:
admin能实现对数据库的增加修改,
这里写图片描述
user:实现对数据库的删除操作,
search:作者在这里留了一个坑,这里写图片描述请各位读者在验证的时候自己修改,

index:这里写图片描述
好了,就这样了。

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值