View only sometimes writes to filesystem.

Here’s my code.

The relevant passage looks like this:

def ingest_full_spec ( request ):
  """ For commentary and simpler illustrations, see the functions
      ingest_json() and upload_multiple() in examples.py.
  """

  log = open ( "/mnt/web/logs/log_outer.txt", "a" )
  log . write ( "Here, it writes to the filesystem.\n" )
  log . close ()

  if request . method == 'POST':
    # ... let's skip this, and call it Part A ...
    if advanced_specs_form . is_valid ():
      log = open ( "/mnt/web/logs/log_inner.txt", "a" )
      log . write ( "But this doesn't get written." )
      # ... let's skip this, and call it Part B ...
      log . close ()
      # ... let's skip this, and call it Part C ...

Before the call to is_valid(), the call to log . write() works. After it, the call does not. The two files log_inner.txt and log_outer.txt both already exist. The code I’m calling Part C still gets executed.

What are you seeing that is convincing you that “Part C” is executing? (I also notice where you’re not writing a newline after your string in your second write, but that shouldn’t matter here.)

Hey Ken! Happy to see you’re still doing the Lord’s work, setting us ignorant heathens onto the path!

Part C includes the following line

      lib.write_form_to_maybe_new_user_folder (
          user_path,
          advanced_specs_form )

That writes a file called shell.json to the filesystem. If I delete shell.json and then visit the view, it appears again. (That appears in views.py, and the function write_form_to_maybe_new_user_folder appears in lib.py.)

The reason I’m trying to write to the log file is because shell.json actually gets written to the wrong place – it’s going to /mnt/tax/users/, whereas should go to a subfolder thereof. I want to write user_path to the log, so I can see what value is being passed to write_form_to_maybe_new_user_folder.

1 Like

Cool, ok.

Is this failing in your development environment or in a production(ish) environment?

If you’re running this locally using runserver, I’d be tempted to toss some print statements in this area to try and get a better view of what’s going on. More likely, I’d want to try and run this in the debugger to examine these variables.

Just based on the name, is “/mnt/web/logs” a “foreign” (‘mounted’, remote, netfs, etc) file system? Any symlinks involved?

(Sorry, I don’t have an immediate answer for you. There’s nothing obvious-to-me wrong with what you have here, so I’m casting a wide net trying to get any sort of clue as to what’s going on. I’m basically fishing for any indication that any component of the path might not be the physical location for the file.)

Looking at the complete project you linked to, your README.md makes a reference to docker. Are you running this in a container? If so, can you run this outside the container to verify behavior?

Sorry I don’t have anything of substance to offer here, but when the code looks ok, I start looking at environmental factors that may be affecting things.

Solved!

The problem was that I was using the same template for two form-submission views. Where the submitted data gets sent, and what happens afterward, is dictated by the action= argument in the template’s HTML form element. The example function I had written was capturing the data from the production-intended function that I had based on the example. That’s why in the production view, code inside the if request.method = POST clause wasn’t being executed, but everything outside of it was.

In future I’ll make the destination an argument sent to the HTML from Django, so that I can’t make that kind of mistake.

Many thanks for your help, Ken! (I know the solution looks orthogonal to your line of questions, but I believe the line helped – I only solved the problem in the course of writing out a different, much longer response to you, which is now irrelevant.)

1 Like