# View only sometimes writes to filesystem.

**URL:** <https://forum.djangoproject.com/t/view-only-sometimes-writes-to-filesystem/6799>\
**Category:** Using Django\
**Created:** [February 27, 2021, 1:41am UTC](https://forum.djangoproject.com/t/view-only-sometimes-writes-to-filesystem/6799 "2021-02-27T01:41:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeffreyBenjaminBrown](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jeffreybenjaminbrown/32/2237_2.png) [@JeffreyBenjaminBrown](https://forum.djangoproject.com/u/JeffreyBenjaminBrown)\
**Post date:** [February 27, 2021, 1:41am UTC](https://forum.djangoproject.com/t/view-only-sometimes-writes-to-filesystem/6799/1 "2021-02-27T01:41:49Z")

</div>

Here’s [my code](https://github.com/ofiscal/tax.co.web/blob/f6b71e8228efd92eb83f293cd1c5055283f4eb45/django/run_make/views/views.py).

The relevant passage looks like this:

```auto
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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 27, 2021, 2:18am UTC](https://forum.djangoproject.com/t/view-only-sometimes-writes-to-filesystem/6799/2 "2021-02-27T02:18:35Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![JeffreyBenjaminBrown](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jeffreybenjaminbrown/32/2237_2.png) [@JeffreyBenjaminBrown](https://forum.djangoproject.com/u/JeffreyBenjaminBrown)\
**Post date:** [February 27, 2021, 5:26am UTC](https://forum.djangoproject.com/t/view-only-sometimes-writes-to-filesystem/6799/3 "2021-02-27T05:26:03Z")

</div>

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

```auto
      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](https://github.com/ofiscal/tax.co.web/blob/f6b71e8228efd92eb83f293cd1c5055283f4eb45/django/run_make/views/views.py), and the function `write_form_to_maybe_new_user_folder` appears in [lib.py](https://github.com/ofiscal/tax.co.web/blob/bug-2021-02-26-user-folder-not-made/django/run_make/views/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`.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 27, 2021, 1:04pm UTC](https://forum.djangoproject.com/t/view-only-sometimes-writes-to-filesystem/6799/4 "2021-02-27T13:04:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JeffreyBenjaminBrown](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jeffreybenjaminbrown/32/2237_2.png) [@JeffreyBenjaminBrown](https://forum.djangoproject.com/u/JeffreyBenjaminBrown)\
**Post date:** [March 1, 2021, 10:57pm UTC](https://forum.djangoproject.com/t/view-only-sometimes-writes-to-filesystem/6799/5 "2021-03-01T22:57:47Z")

</div>

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.)
