Is there a better way to structure post methods in Class Based Views -
i'm using class based views in django , never specify url in ajax calls , omit action parameter in form because know these requests go through post method of class based view. notice view - profile page make many requests depending on user updating on page - post method become convoluted , if/else mess example:
def post(self, request, *args, **kwargs): # handle user changing available date if request.post.get('availabledate') != none: self.updateavailabledate(request) return jsonresponse({'result':'success'}) if request.post.get('newprojectname') != none: creative_user = creativeuserprofile.objects.get(id = request.user.id) project = project.create_project(creative_user, request.post['newprojectname']) project.save() return jsonresponse({'projectid': project.id }) if request.post.get('projectimage') != none , request.files['file']: project_id = request.post['projectimage'] # project , associate uploaded image project = get_object_or_404(project, id = project_id) projectimage = projectimage() projectimage.file = request.files['file'] projectimage.project = project projectimage.title = "" image_form = projectimageform(request.post, instance = projectimage) if image_form.is_valid(): image_form.save() else: print (image_form.errors); return jsonresponse({'result':'success'}) # delete project if has no images if request.post.get('removeprojectid') != none: project_id = request.post['removeprojectid'] project = get_object_or_404(project, id = project_id) images = projectimage.objects.filter(project_id = project.id) if not images: project.delete(); return jsonresponse({'result':'success'})
is there better way make disparate posts django avoiding multiple if statements example, each ajax or form submission being able target specific function?
thanks
Comments
Post a Comment