I suppose in a view, you can write something along these lines:
import json
from os import path
from django.conf import settings
def index(request):
if request.method == ‘POST’:
form = YourForm(request.POST)
if form.is_valid():
field1 = form.cleaned_data.get(‘field1_from_form’)
# write your JSON here
# Follow this tutorial to write your json:
https://stackabuse.com/reading-and-writing-json-to-a-file-in-python/
# Instead of dumping your file into a txt like they did in the
first example, do this:
with open(path.join(settings.MEDIA_ROOT, “ajsonfile.json”),
“w+”) as outfile:
json.dump(data, outfile)
return redirect(request.get_absolute_uri())
elif request.method == “GET”:
form = YourForm()
return render(request, “index.html”, {‘form’: form}
I forgot the import statement for the redirect
On Sat, Aug 10, 2019 at 6:28 AM Suraj Thapa FC <[email protected]>
wrote:
> How do I write my front-end form response in.. .json file
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAPjsHcG4%3D%3D%2BRWSeDocSiF9dGaHdt4LFi%2B-AB11Q43vOhZnY1Zg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAPjsHcG4%3D%3D%2BRWSeDocSiF9dGaHdt4LFi%2B-AB11Q43vOhZnY1Zg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAJVmkN%3Dep5tSFa01XjfvyZdhhzVSXekcpkC1Wx1U-xR2cjP9ww%40mail.gmail.com.