V
V
Val1n0r2019-09-19 11:40:40
Django
Val1n0r, 2019-09-19 11:40:40

How to import a class from another Django application?

Actually the problem:
There is an application in jung called 'stats' (the purpose of the application is to pull values ​​from the database and connect it when necessary)
This is what the view looks like, to which I need global access in the entire project
stats views.py

class ShowStats(View):
  def get(self,request):
    if request.user.is_authenticated:
      global auser,UserGold ,UserKnights,UserXp,UserEnergy,UserPower,UserLevel,status,xpfl,progress
      auser = request.user.id

      GetStat = UserAttribute.objects.filter(user_id = auser)
      for s in GetStat:
        UserPower = s.power
        UserGold = s.gold
        UserKnights = s.knights
        UserXp = s.xp
        UserLevel = s.level
        UserEnergy = s.energy
        status = s.isComplete


      mylvl =  UserLVL.objects.raw("SELECT lvl FROM userlevel_userlvl WHERE lvlxp - {}<=0".format(UserXp))
      for j in mylvl:
          cl = j.lvl


      xpforlevel =  UserLVL.objects.raw("SELECT lvl,lvlxp FROM userlevel_userlvl WHERE lvl={}".format(UserLevel))
      for k in xpforlevel:
          xpfl = k.lvlxp
      if UserXp >= xpfl:
          UserAttribute.objects.filter(user_id=auser).update(level=F('level')+1,xp=F('xp')-UserXp)

      progress = decimal.Decimal(round((UserXp/xpfl)*100,0))



    #setLevel = UserAttribute.objects.filter(user_id = auser).update(level=cl)
      return auser,UserGold,UserKnights,UserXp,UserLevel,UserEnergy,UserPower,status,xpfl,progress

    else:
      return redirect('index')

This is how I import it . I call the method already in another view and, in theory, should get the values ​​​​from return, but return does not return anything.from game.views import ShowStats
class FarmIndex(View):
    def get(self,request):
        usr = request.user.id
        ShowStats.get(self,request)
        
        context = {
          'UserGold':UserGold,'UserKnights':UserKnights,'UserXp':UserXp,'UserLevel':UserLevel,
          'UserEnergy':UserEnergy,'UserPower':UserPower,'status':status,'xpfl':xpfl,'progress':progress,
          }

        return render(request,'farm.html',context)

If I insert a class that pulls values ​​into the view in which I call this method, everything works. When importing - no (
I suspect that it if request.user.is_authenticated:returns False at the beginning. I
just want to violate the DRY principle to a minimum
. Help, please.
PS code is shit)) But I'm learning

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2019-09-19
@fox_12

Something with the design of the code you generally have trouble ...
Why globals? Have you heard of pep8? The very naming of variables, and the code itself...
Move the logic into a separate function, import it and use it in your views...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question