• Xadmin添加,编辑,删除


     Xadmin添加,编辑,删除

    1.HTML

    编辑和添加页面得内容相同,使用include将他们整合
    {% include xxx.html %} 获取指定页面的所有内容
    1.单独建个html存放编辑和添加页面得代码:
    '''
    forms.HTML
    <form action="" method="post" novalidate >
        {% csrf_token %}
        {% for foo in form_obj %}
            <div class="form-group">
                <label for="">{{ foo.label }}</label>
                {{  foo }}  <span> {{  foo.errors.0}}</span>
            </div>
        {% endfor %}
        <input type="submit" class="btn btn-default">
    </form>
    
    
    '''
    编辑和添加页面
    <div class="container " style="margin-top: 30px" >
        <div class="row">
            <div class="col-md-4 col-md-offset-3">
                {% include 'forms.html' %}
            </div>
        </div>
    </div>

    2.views.py

    add

     def add_view(self, request):
            # list_url="%s/%s/"%(self.app_name,self.model_name)
            DemoModelForm=self.get_model_form_class() #得到定义的ModelForm组件类
            if request.method=='POST':
                form_obj=DemoModelForm(request.POST)
                if form_obj.is_valid():
                    form_obj.save()
                    # return redirect(list_url)  #不能用,相当于在当前路径后面拼接
                    return redirect(self.get_list_url())
                else:
                    return render(request,'add_view.html',locals())
            form_obj = DemoModelForm()
            return render(request, 'add_view.html', locals())

    edit

        def change_view(self, request, id):
            DemoModelForm = self.get_model_form_class()  # 得到定义的ModelForm组件类
            change_obj=self.model.objects.get(pk=id)
            if request.method == 'POST':
                form_obj = DemoModelForm(request.POST,instance=change_obj)
                if form_obj.is_valid():
                    form_obj.save()
                    # return redirect(list_url)  #不能用,相当于在当前路径后面拼接
                    return redirect(self.get_list_url())
                else:
                    return render(request, 'change_view.html', locals())
            form_obj = DemoModelForm(instance=change_obj)
            return render(request, 'change_view.html', locals())

    del

     def del_view(self, request, id):
            if request.method=='POST':
                self.model.objects.get(pk=id).delete()
                return redirect(self.get_list_url())
            return render(request,"del_view.html",{"url":self.get_list_url()})
     
  • 相关阅读:
    初识beego
    前端代码
    实现步骤: 跳转-分享-登录-通信录
    实现步骤: 推送&传感器&UIDynamic
    地图显示的步骤
    stackView的隐藏与显示注意事项
    iOS开发的知名大牛博客小汇
    Hidden File For Mac
    file:///Users/xmg/Desktop/xiangmu~Bsbdejie/BaisibudejieTheSecondtime/BaisibudejieTheSecond/BaisibudejieTheSecond/AppDelegate.m: warning: Missing file: /Users/xmg/Desktop/xiangmu~Bsbdejie/BaisibudejieT
    保存相册以及保存图片完成的时候调用
  • 原文地址:https://www.cnblogs.com/chongdongxiaoyu/p/9177796.html
Copyright © 2020-2023  润新知