Laravel's dd() equivalent in django

后端 未结 3 983
青春惊慌失措
青春惊慌失措 2021-01-01 17:11

I am new in Django and having a hard time figuring out how to print what an object have inside. I mean type and value of the variable with its members inside. Just like Lara

3条回答
  •  执笔经年
    2021-01-01 17:57

    Actually Django does not provide this specialized function. So in order to get rid of this problem, I made a custom dd() type function and use this in all Django projects. Perhaps it can help someone.

    Let's assume, we have a library folder named app_libs and in that folder we have a library file named dump.py. Like app_libs > dump.py:

    from django.core import serializers
    from collections.abc import Iterable
    from django.db.models.query import QuerySet
    from django.core.exceptions import ObjectDoesNotExist
    
    
    def dd(request, data=''):
        try:
            scheme      = request.scheme
            server_name = request.META['SERVER_NAME']
            server_port = request.META['SERVER_PORT']
            remote_addr = request.META['REMOTE_ADDR']
            user_agent  = request.META['HTTP_USER_AGENT']
            path        = request.path
            method      = request.method
            session     = request.session
            cookies     = request.COOKIES
    
            get_data = {}
            for key, value in request.GET.lists():
                get_data[key] = value
    
            post_data = {}
            for key, value in request.POST.lists():
                post_data[key] = value
    
            files = {}
            for key, value in request.FILES.lists():
                files['name'] = request.FILES[key].name
                files['content_type'] = request.FILES[key].content_type
                files['size'] = request.FILES[key].size
    
            dump_data = ''
            query_data = ''
            executed_query = ''
            if data:
                if isinstance(data, Iterable):
                    if isinstance(data, QuerySet):
                        executed_query = data.query
                        query_data = serializers.serialize('json', data)
                    else:
                        dump_data = dict(data)
                else:
                    query_data = serializers.serialize('json', [data])
    
    
            msg = f'''
                
                    Scheme        : {scheme}
    Server Name : {server_name}
    Server Port : {server_port}
    Remote Address: {remote_addr}
    User Agent : {user_agent}
    Path : {path}
    Method : {method}
    Session : {session}
    Cookies : {cookies}
    Get Data : {get_data}
    Post Data : {post_data}
    Files : {files}
    Executed Query:
    {executed_query}

    Query Data :
    {query_data}

    Dump Data :
    {dump_data}

    ''' return msg except ObjectDoesNotExist: return False

    when you need to use this function, just call it like this in any views.py:

    from django.http import HttpResponse
    from django.shortcuts import render
    from django.views import View
    
    from app_libs.dump import dd
    from .models import Products
    
    class ProductView(View):
        def get(self, request):
            data = {}
            data['page_title'] = 'products'
            data['products'] = Products.objects.get_all_product()
    
            template = 'products/collections.html'
    
            dump_data = dd(request, data['products'])
            return HttpResponse(dump_data)
    
            # return render(request, template, data)
    

    that's it.

提交回复
热议问题