Sunday, December 22, 2019

Django admin app recreated.

Hello Guys, thank you for visiting my blog. This project is just a replica of default admin app in django-frame work. Anyone can make one. The link of the source code can be found below.


Django Admin App

An admin page is basically a page that contains all the details stored in your data. Django provides us a default admin app. You can visit this app by going to www.yourwebip.com/admin . 

You can be the superuser of the admin app by typing

py manage.py createsuperuser

Informations about my project.

Extra-Modules Created:

1 . Mod.py

Mod py conatins two functions getclass(), getmembers().

1.getclass()

This function is used to get the names of all the classes in models.py . This function uses "inspect", which is used to get get the details of class. So by this i can get the names of the all classes. Here is the code of the function getclass.

def getclass():
    p = inspect.getmembers(models,inspect.isclass)
    l = []
    print(p)
    for key, data in inspect.getmembers(models, inspect.isclass):
        print('{} : {!r}'.format(key,data))
        p1 ='{}'.format(key)
        l.append(p1)
    return l


2.getmembers()

This function works about same as getclass. What is the difference is that it used to get the members of the class. That is inside models we are creating some rows, and it will get the names of the members. Here is the code of the function getmembers().

def getmembers(mclass):
    example = mclass
    members = [attr for attr in dir(example) if not callable(getattr(example, attr)) and not attr.startswith("_")]
    print(members)
    return members

So these are the details of the extra created module mod.py

Some Important Functions in Views:

1. test(request).

def test(request):
    p = getclass()
    return render(request, "admin.html", {"l":p})

This function simple get the names of models using getclass(), function. And displays them by passing it through the dictionary of render.

2. details()

def details(requestdet):
    print(det,type(det))
    p = getclass()
    x = eval(det)
    print(x, type(x),"gfggggggggggggggggggggggggggggggggggggggggggggggggggg")
    f = x.objects.all()
    print(f)
    return render(request, "adetails.html", {"l":p, "f":f, "g":det})

This function is simply to get the details of data's stored in a model. det is variable that gets the model name through the run. I use the eval() function to convert the string into python expressions. and "f" gets the data's stored in it. And Finally it displays.

3. Info()

def info(requestdetid):
    va = {}
    y = id
    x = eval(det)
    f = x.objects.get(id = y)
    p = getclass()
    d = getmembers(x)
    k = x.objects.all().values()
    for ite in k:
        for ui,vi in ite.items():
            if ui == "id" and vi == int(y):
                va = ite
    f = va
    mname = det
    return render(request, "ainfo.html", {"l":p, "g":d, "j":f, "i":y, "c":mname})

This function simply shows the information of the clicked data. If you have any doubts regarding this, you can comment here. Just go through the code its easy to understand. If you don't get it i can help.

4. adminadd()

def adminadd(request,det):
    l = []
    if request.method == "POST":
        word = det + "("
        x = eval(det)
        fld = getmembers(x)
        for y in fld:
            if y == 'id' or y == 'pk' or y == 'objects' or y == 'user':
                continue                
            else:
                an = request.POST.get(y)
                print(an, type(an),"+++++++++++++++++++++++++++++++++++++++++")         
                word = word + y
                word = word + "=" +'"' + an + '"' + ", "
                l.append(request.POST.get(y))

        word = word + ")"   
        print(l)
        print(word)
        x = eval(word)
        x.save()

        return redirect(test)
    else:
        x = eval(det)
        fld = getmembers(x)
        p = getclass()
        usd = User.objects.all()
        return render(request, "adminadd.html", {"l":p, "g":fld, "c":det, "usr":usd})

This function is to add data's to the database. What it is really doing is to get the values such as model name etc.. in the form of string and use eval function to convert it into python expression so simple.


So these are some of the important functions to know.


Please support me for my efforts: https://paypal.me/matneb


Download the source Code here :

No comments:

Post a Comment

Comments system