In my app I am trying to save the database data into a JSON file.
Here is my views.py:
if request.POST:
form = BookForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
form.save()
JSONSerializer = serializers.get_serializer("json")
json_serializer = JSONSerializer()
with open("book.json", "w") as out:
json_serializer.serialize(Book.objects.all(), stream=out)
return redirect('/index/')
return render_to_response('addbook.html',{ 'form':form },context_instance=RequestContext(request))
I am using serializer for doing that.
The problem is that the data is saved in the database, but not written to the file.
I am getting the following error,while running the above code
IOError at /addbook/
[Errno 2] No such file or directory: 'fixtures/book.json'
Request Method: POST
Request URL: http://localhost:8000/addbook/
Django Version: 1.3.7
Exception Type: IOError
Exception Value:
[Errno 2] No such file or directory: 'fixtures/book.json'
Exception Location: /root/Samples/DemoApp/DemoApp/views.py in addbook, line 53
Python Executable: /usr/bin/python
Python Version: 2.7.0
path = "{0}/app_name/fixtures/book.json".format(settings.PROJECT_ROOT)
with open(path, "w") as out:
json_serializer.serialize(Book.objects.all(), stream=out)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.