0

I can't get my code to save a new entry and return an error message if entry already existed. its just the views.py I can't figure out help please. code belowenter image description here

0

There might be more than one way of doing this, but I imported default_storage and ContentFile and it worked for me.

from django.core.files.storage import default_storage

from django.core.files.base import ContentFile

And then in my add function

file = ContentFile(content)

default_storage.save( f"entries/{title}.md", file)

return HttpResponseRedirect(title)

It looks like you've got some cut'n'paste code hanging around from the Tasks app. tasks.append(title) will add title to a list of tasks. I think the objective though is to save the content to a title.md markdown file in the entries folder of the project.

Finally, you need to check if the title already exists and only perform the save if it doesn't. Something like: if (title in util.list_entries()) should be a good start.

3
  • hey thanks. but I need it to add and save entry – Gaza Jul 25 '20 at 19:09
  • replace the two lines tasks.append(title) and tasks.append(content) with util.save_entry(title, content). Then your first return statement will be return HttpResponseRedirect(title). Make sure you put django.http import HttpResponseRedirect at the top of the views.py – Brian Jul 26 '20 at 10:38
  • thanks a lot helped a lot – Gaza Jul 26 '20 at 17:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .