zoukankan      html  css  js  c++  java
  • django_day11_项目相关

    django_day11_项目相关

    新增和编辑

    路由

    url(r'^category_add/$', views.category_change, name='category_add'),
    url(r'^category_edit/(d+)$', views.category_change, name='category_edit'),
    

    视图

    def category_change(request, pk=None):
        obj = models.Category.objects.filter(pk=pk).first()  # pk=None   obj=>None
        form_obj = CategoryForm(instance=obj)
        if request.method == 'POST':
            form_obj = CategoryForm(request.POST, instance=obj)
            if form_obj.is_valid():
                form_obj.save()
                return redirect('category_list')
    
        title = '编辑分类' if pk else '新增分类'
    
        return render(request, 'form.html', {'form_obj': form_obj, 'title': title})
    

    模板

    {% extends 'dashboard.html' %}
    
    
    {% block main %}
        <h1>{{ title }}</h1>
    
        <form class="form-horizontal" method="post" action="" novalidate>
            {% csrf_token %}
    
            {% for field in form_obj %}
                <div class="form-group {% if field.errors %}has-error{% endif %}">
    
                    <label for="{{ field.id_for_label }}"
                           class="col-sm-2 control-label">{{ field.label }}</label>
                    <div class="col-sm-8">
                        {{ field }}
                        <span class="help-block"> {{ field.errors.0 }} </span>
                    </div>
                </div>
            {% endfor %}
    
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-default">保存</button>
                </div>
            </div>
        </form>
    
    {% endblock %}
    
    

    Q

    q = Q()
    # Q(Q(title__contains=query)|Q(detail__content__contains=query))
    q.connector = 'OR'
    q.children.append(Q(title__contains=query))
    q.children.append(Q(detail__content__contains=query))
    
    
    Q(title__contains=query)  #   Q(('title__contains',query))
     
    
    
    def get_query(request, field_list):
        # 传入一个列表['title','detail__content'] ,返回一Q对象
        query = request.GET.get('query', '')
    
        q = Q()
        # Q(Q(title__contains=query)|Q(detail__content__contains=query))
        q.connector = 'OR'
        for field in field_list:
    
            q.children.append(Q(('{}__contains'.format(field),query)))
        # q.children.append(Q(detail__content__contains=query))
    
        return q
    
    

    解决分页保留查询参数的方法

    from django.http.request import QueryDict
    
    request.GET._mutable = True
    request.GET['page'] = 1
    
    request.GET.copy()  # 返回一个可编辑的深拷贝
    
    request.GET.urledncode()  # page=1&aa=111
    
  • 相关阅读:
    (转)当别人努力的时候,你在做什么?
    《IT项目管理》读书笔记(9) —— 项目风险管理
    线程通信机制之定时器队列
    处理控制台事件消息
    C++常见内存错误及解决方案
    WCF与现行分布式通讯技术性能对比
    (译)如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
    常用性能计数器说明
    有关WCF公布IDataRead的问题
    负载均衡
  • 原文地址:https://www.cnblogs.com/DemoLi/p/12770282.html
Copyright © 2011-2022 走看看