O
O
ooker2020-10-05 21:00:20
Django
ooker, 2020-10-05 21:00:20

How to shorten django code?

Good afternoon! I'm a newbie and can't find options.
The front is made on the site, the client selects the activity and the choice falls into the form wrapper. There is no way to place django forms in the wrapper. I save by input name. How can you optimize your code

class MasterProfile(generic.View):

    def post(self, request):
        razdel1 = HairCut.objects.all()
        uslugi_haircut = HaircutUslugi()
        razdel2 = SpaCentre.objects.all()
        ctx = {
            'razdel1': razdel1,
            'uslugi_haircut': uslugi_haircut,
            'razdel2': razdel2
        }
        if request.method == "POST":
            data = HairCut()
            data.deyatelnost1 = request.POST.get('deyatelnost1')
            data.save()
            data = HaircutUslugi()
            data.usluga1 = request.POST.get('usluga1')
            data.save()
            data = SpaCentre()
            data.deyatelnost2 = request.POST.get('deyatelnost2')
            data.save()
            data = Cosmetologia()
            data.deyatelnost3 = request.POST.get('deyatelnost3')
            data.save()
            data = Makeup()
            data.deyatelnost4 = request.POST.get('deyatelnost4')
            data.save()
            data = Nails()
            data.deyatelnost5 = request.POST.get('deyatelnost5')
            data.save()
            data = Tatoo()
            data.deyatelnost6 = request.POST.get('deyatelnost6')
            data.save()
            data = Massage()
            data.deyatelnost7 = request.POST.get('deyatelnost7')
            data.save()
            data = Depilation()
            data.deyatelnost8 = request.POST.get('deyatelnost8')
            data.save()
        return render(request, "masterregistration/masterprofilestart.html", ctx)

    def get(self, request,):
        razdel1 = HairCut.objects.all()
        uslugi_haircut = HaircutUslugi()
        razdel2 = SpaCentre.objects.all()
        ctx = {
            'razdel1': razdel1,
            'uslugi_haircut': uslugi_haircut,
            'razdel2': razdel2
        }
        return render(request, "masterregistration/masterprofilestart.html", ctx)

Thanks for the answer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bituke, 2020-10-06
@bituke

Writing business logic in an application view is, in principle, a big mistake for most beginners.
We write all the request-response logic in a separate file, import it into a view and use it with the help of certain variables.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question