Как выполнить более одной операции в представлении функции django - PullRequest
0 голосов
/ 27 мая 2019

Я пытаюсь выполнить 2 операции с одним представлением функции. Но он выполняет только первую операцию. Какую операцию я упомянул в первую очередь только, что операция выполнена, вторая операция не выполнена. Любой другой способ решить эту проблему.

def home_view(request):
    if 'username' in request.session:
        if 'username' in request.session:
            username = request.session['username']
            business_objs = AddBusiness.objects.all().values()
            return render(request, 'home/index.html', {'business_objs': business_objs})
        elif request.method == 'GET':
            username = request.session['username']
            form = ProfileForm(request.POST)
            if form.is_valid():
                profile_info = Profile.objects.filter(username=username).values()
                for i in profile_info:
                    profiledict = i
                    return render(request, 'home/index.html',
                                  {'profile_first_name': profiledict['first_name'],
                                   'profile_last_name': profiledict["last_name"],
                                   'profile_phone_number': profiledict['phone_number'],
                                   'profile_email': profiledict['email'], 'profile_address': profiledict['address'],
                                   'profile_image': profiledict['image']})
                return redirect('/home/')
            return redirect('/home/')

    else:
        return redirect('/login/')

1 Ответ

1 голос
/ 27 мая 2019

Я думаю, вы можете попробовать вот так:

def home_view(request):
    if 'username' in request.session:
        if <b>'request.method == 'GET'</b>:
            username = request.session['username']
            business_objs = AddBusiness.objects.all().values()
            return render(request, 'home/index.html', {'business_objs': business_objs})
        elif <b>request.method == 'POST'</b>:
            username = request.session['username']
            form = ProfileForm(request.POST)
            if form.is_valid():
                profile_info = Profile.objects.filter(username=username).values()
                for i in profile_info:
                    profiledict = i
                    return render(request, 'home/index.html',
                                  {'profile_first_name': profiledict['first_name'],
                                   'profile_last_name': profiledict["last_name"],
                                   'profile_phone_number': profiledict['phone_number'],
                                   'profile_email': profiledict['email'], 'profile_address': profiledict['address'],
                                   'profile_image': profiledict['image']})
                return redirect('/home/')
            return redirect('/home/')

    else:
        return redirect('/login/')

Таким образом, вы сможете обрабатывать запросы GET и POST, используя это представление на основе функций.Дополнительную информацию можно найти в documentation.

...