如何为单页编写多视图功能

时间:2019-05-27 11:35:59

标签: django django-views

我正在尝试对单个网页执行多项操作。我正在努力为单个网页编写多功能视图。怎么做。帮帮我。

def home_view(request):
   if 'username' in request.session:
        if request.method == 'GET':
             form = ProfileForm(request.GET)
             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']})
         elif request.method == 'POST':
             first_name = request.POST.get('first_name')
             last_name = request.POST.get('last_name')
             phone_number = request.POST.get('phone_number')
             email = request.POST.get('email')
             address = request.POST.get('address')
             image = request.FILES['images']
             file_storage = FileSystemStorage()
             obj = Profile.objects.all().filter(username=username)
             if len(obj) > 0:
                 obj.update(first_name=first_name, last_name=last_name, phone_number=phone_number,
                               email=email, address=address, image=file_storage.save(image.name, image))
                 return redirect('/home/')
             return redirect('/home/')
    else:
        return redirect('/login/')

def home2_view(request):
    if 'username' in request.session:
          business_objs = AddBusiness.objects.all().values()
          return render(request, 'home/index.html', {'business_objs': business_objs})
    else:
        return redirect('/login/')

我这样尝试过,它仅执行第一个操作。在这里,我尝试执行3个操作:GET(从数据库中提取数据并以HTML表单显示),POST(更新表单数据)以及在HTML页面中显示所有数据库行和列。

0 个答案:

没有答案