-3

I am currently working on my first website and I'm working with class-based views because I've been told their inheritance is outstanding.

Here's the code so you can make yourselves an idea of what I'm trying to do before I tell you the error I'm having.

class TranslatorView(View):

    def translate_amino(self, codon):
        return self.amino_mapper.get(codon, "")

    def build_protein(self, phrase):
        protein = []
        i = 0
        while i < len(phrase):
            codon = phrase[i: i + 3]
            amino = self.translate_amino(codon)
            if amino:
                protein.append(amino)
            else:
                print(f"The codon {codon} is not in self.mapper_1")
            i += 3
        return protein
    
    def error_1(self, request):
        amino_1= self.build_protein(request.GET.get('phrase',''))
        if len(amino_1) % 3:
            messages.error(request, "DNA chain must be divisible by 3")
            return redirect('/')


Basically, what I'm trying to do is to inherit the function build_protein to the def error_1. That's why I create a variable called amino_1 equal to the def build_protein. Afterwards, I create a condition saying if amino_1 detects a phrase that it's a total number of letters aren't divisible by three, it should raise a value error. However, it appears it's not inheriting anything at all.

  • `self.build_protein` is a function ... what do you think `amino_1= self.build_protein` does? And what should `if len(amino_1) % 3:` do? – Patrick Artner Nov 30 '20 at 10:38
  • 1
    note sure but that might be something like `amino_1 = self.build_protein( _ some value from your request that is a RNA chain as string _ )` what you are after? – Patrick Artner Nov 30 '20 at 10:39
  • I've edited the post to see if I make myself more clear about what I want to do. Hope it's helpful – MarcJuegos_Yt Nov 30 '20 at 10:41
  • amino_1 just becomes a callback, though you need to pass some parameter to it. If you print amino_1 it will most likely say bound method, what will len(bound method) do? – danish_wani Nov 30 '20 at 10:48
  • so what parameter should I print in order to inherit the phrase parameter from the build_protein function – MarcJuegos_Yt Nov 30 '20 at 10:52

1 Answers1

0

It is about your problem (you have to call build_protein method):

amino_1= self.build_protein(request.GET.get('phrase',''))

look how to capture URL parameters in request.GET here

About class based views and inheritance ... seems like you don`t understand these terms, so first look for OOP basics then read django docs about class based views and their usecases

madzohan
  • 11,488
  • 9
  • 40
  • 67