zoukankan      html  css  js  c++  java
  • python:django

      ====启动django====

    python manager.py runserver --host 0.0.0.0 --port 9008

    python manager.py runserver --help可以查看所有的选项

    python manager.py runserver --host 0.0.0.0 --port 9008 --setting=fms/settings_dev

    这里可以用setting变量,设置环境信息,比如dev,test,production等


    ====1【request】http输入层====

    【request获取GET参数】
    id = request.GET.get('id')
    print(id)

    【request获取POST参数】

    name = request.GET.get('name')
    print(name)
    
    #获取复选框的多个值
    ids = request.POST.getList('ids[]')
    print ids
    
    #打印到文件
    fp = open(file, 'a+')
    for id in ids:
        fp.write(str(id) + "
    ")
    fp.close()

    ====2【模板】django模板====

     (16)spaceless 使用形式:{% spaceless %} (内容) {% endspaceless %} 意义:删除包围内容中的所有tab或者回车字符。

    (22)linebreaks 使用形式:{{value|linebreaks}} 意义:value中的" "将被<br/>替代,并且整个value使用</p>包围起来,从而适和HTML的格式
    (23)linebreaksbr 使用形式:{{value |linebreaksbr}} 意义:value中的" "将被<br/>替代

    ===关闭了Django的自动转义===
    我们还可以通过{%autoescape off%}的方式关闭整段代码的自动转义,比如下面这样:

    {% autoescape off %}
    Hello {{ name }}
    {% endautoescape %}

    如果choices是数组,[0,1,2,3]
        {% for choice in choices %}
                {{choice.choice}} - {{choice.votes}} <br />
        {% endfor %}
    
    如果choices是dict,{a:1, b:2, c:3}
        {% for k,v in choices.items %}
        <option value="{{k}}">{{v}}</option>
        {% endfor %}
    循环遍历参考文章:https://docs.djangoproject.com/en/2.1/ref/templates/builtins/

    ====3【response响应】====

    【response】render和render_to_reponse区别

    https://www.cnblogs.com/leonwen/p/5368239.html

    如何将数据化后的模板返回到string中在程序中使用

    from django.template.loader import render_to_string
    str = render_to_string('f/project_search_list.html', data)

    ====4【DB】django db操作====

    db = Project(project_id=1, content_id=1)
    db.save()
    #return

    db = Project.objects.filter(project_id=1)
    db.delete()
    return

    [DB细节]获取insert后的id

    >>> b2 = Blog(name='Cheddar Talk', tagline='Thoughts on cheese.')
    >>> b2.id     # Returns None, because b doesn't have an ID yet.
    >>> b2.save()
    >>> b2.id     # Returns the ID of your new object.

    #有判断插入是否成功
    #假设b2.id是UUID类型,则使用如下判断
    if str(b2.id) == '':
      insert_id = b2.id
    #假设b2.id是int类型,且设置了默认值是0,则使用如下判断
    if int(b2.id) == 0:
      insert_id = b2.id

     参考链接:https://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs#auto-incrementing-primary-keys

    【DB】多个搜索条件(包含like)

    object = Project.objects
    if request.GET.get('name'):
        object = object.filter(name__contains=request.GET.get('name'))
    if request.GET.get('domain'):
        object = object.filter(domain__contains=request.GET.get('domain'))
    if request.GET.get('system_type') and request.GET.get('system_type') != '0':
        object = object.filter(system_type=int(request.GET.get('system_type')))
    if request.GET.get('project_depart')  and request.GET.get('project_depart') != '0':
        object = object.filter(project_depart=int(request.GET.get('project_depart')))
    if request.GET.get('level')  and request.GET.get('system_type') != '0':
        object = object.filter(level=int(request.GET.get('level')))
    if request.GET.get('project_leader'):
        object = object.filter(project_leader__contains=request.GET.get('project_leader'))
    
    Projects = object.all()
    
    #这里需要注意两点:1,每次filter之后,都需要重新赋值到object;2,如果有like操作,则要使用field_name__contains这个约定写法来完成like(这里包含两个下划线,不是一个);

    [DB细节]更新逻辑

    【model层写逻辑】
    ----mysql update---
    patient_edit = PatientInfo.objects.get(id=patient_id) # object to update
    patient_edit.name = 'New name' # update name
    patient_edit.save() # save object

    https://my.oschina.net/esdn/blog/803135

    ====5【service】业务逻辑层====

    【service】其它

    @login_required
    @permission_required('accounts.add_project',raise_exception=True)
    def project_add(request):
    
    if request.method == "POST":
    form = ProjectForm(request.POST)
    print(form)
    if form.is_valid():
    form.save()
    else:
    form = ProjectForm()
    return HttpResponseRedirect(reverse('project_list'))
    
    以上代码段中有几个关键函数,post传递参数,然后保存到数据库中
    ProjectForm来组form数据,form.save()保存在数据库中
    
    我们不需要组数据,我们需要的提取数据的信息
    
    charField
    Z:devprojectgitfms.xin.comfms.xin.comvenvlibpython3.6site-packagesdjangodbmodelsfields\__init__.py
    
    django中对字段的设定,哪些是必填的,哪些是可以为空的
    https://www.cnblogs.com/linxiyue/p/3662535.html
    https://django-chinese-doc.readthedocs.io/zh_CN/latest/ref/models/fields.html

    ====附录,python文件操作====

    http://www.runoob.com/python/python-files-io.html

  • 相关阅读:
    高级特性(4)- 数据库编程
    UVA Jin Ge Jin Qu hao 12563
    UVA 116 Unidirectional TSP
    HDU 2224 The shortest path
    poj 2677 Tour
    【算法学习】双调欧几里得旅行商问题(动态规划)
    南洋理工大学 ACM 在线评测系统 矩形嵌套
    UVA The Tower of Babylon
    uva A Spy in the Metro(洛谷 P2583 地铁间谍)
    洛谷 P1095 守望者的逃离
  • 原文地址:https://www.cnblogs.com/helww/p/9670651.html
Copyright © 2011-2022 走看看