jeudi 31 mai 2018

Best way to code multiple decisions in Python

I have the following practical problem (related to Django/Python).

I'm trying to get the following in the most efficient piece of Python code ->

There are 2 items to be checked:

  1. Is the user logged in? if not show a login page, else check if the request is a post request.
  2. Is the request a post request? If not show a form, else handle the form

    def upload(request):
    if request.user.is_authenticated:
        if request.method == 'POST':
        form = forms.DocumentForm()
        return HttpResponse('Handle POST and LoggedIn Prefix Form Validation')
    #return (request, 'upload.html', {'form': form})
    else:
        return HttpResponse('Not Logged In Need to Make Error Landing Page')
    else:
    if request.method == 'POST':
        return HttpResponse('POST but not logged in')
            #return render(request, 'upload.html', {'form': form})
    
    

Aucun commentaire:

Enregistrer un commentaire