• 【django】admin 后台自定义按钮 传参


    class NongYeInfomation(models.Model):
        """
        农业资讯
        """
        title = models.CharField(max_length=255,verbose_name="标题")
        url = models.CharField(max_length=255,verbose_name="路由")
        create_time = models.DateTimeField(auto_now=True,verbose_name="注册时间")
    
        def __str__(self):
            return self.title
    
        def href_route_blank(self):
            path = self.url
    
            button_html = "<a  href='{}' target='_blank'>查看详情{}</a>".format(path,self.pk)
    
            return format_html(button_html)
    
        href_route_blank.short_description = format_html("""<a  href='#' style="position: relative;left: -12px;">新窗口查看</a>""")
    
        def href_route(self):
            path = self.url
    
            # button_html = "<a  href='{}' >查看详情</a>".format(path)
            button_html = "<a  href='/number/{}' >测试数字</a>".format(self.pk)
    
            return format_html(button_html)
    
        href_route.short_description = format_html("""<a  href='#' style="position: relative;left: -12px;">本窗口查看</a>""")
    
    
    
        def data_update(self):
            button_html = ""
    
            return format_html(button_html)
    
        data_update.short_description = format_html(
            """<a  href='nongye/get' style="position: relative;left: -12px;">更新数据</a>""")
    
    
    
        class Meta:
            verbose_name = "农业资讯"
            verbose_name_plural = verbose_name
            db_table = 'NongYeInfomation'






    # 测试获取id
    url(r'number/(?P<ID>.*)$', number.NumberGet.as_view()),


    class NumberGet(APIView):
    def get(self,request,ID):

    message = {}
    try:

    print("ID",ID)

    message['code'] = 200
    message['message'] = "ID是{}".format(ID)
    return JsonResponse(message)
    except Exception as e:
    print(traceback.format_exc())
    msg = traceback.format_exc()
    print(msg)
    message['code'] = 444
    message['message'] = "更新失败"
    return JsonResponse(message)






     

    主攻:Python 数据分析 web 机器学习 图像识别。。 副攻:JAVA WEB 安卓 大数据
  • 相关阅读:
    派生类的构造函数
    继承和派生
    自增自减运算符的重载(强制类型转换运算符重载)
    流插入和流提取运算符的重载
    动态数组类的设计
    函数的返回值
    赋值运算符的重载
    运算符重载
    常量对象函数引用和参数传递
    理解ASP.NET MVC的路由系统
  • 原文地址:https://www.cnblogs.com/wanghong1994/p/13666113.html
Copyright © 2020-2023  润新知