This repository has been archived by the owner on Sep 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
14 Edit existing notes
scotwk edited this page May 10, 2015
·
1 revision
We have the ability to create new notes, but there is no way currently to edit existing notes.
Modify your views
from django.views.generic import ListView, DetailView, CreateView, UpdateView
...
class NoteUpdate(LoginRequiredMixin, UpdateView):
model = Note
form_class = NoteForm
template_name = 'note/form.html'
success_url = reverse_lazy('note:index')
def form_valid(self, form):
form.instance.pub_date = timezone.now()
return super(NoteUpdate, self).form_valid(form)
And whenever you add a new view you need to make a URL for it:
url(r'^(?P<pk>[0-9]+)/edit/$', NoteUpdate.as_view(), name='update'),
We'll use the same template - note/form.html
for both creating and updating view. Update the action button to POST to the appropriate URL based on whether we are creating or updating. If we are updating, object
will be defined.
{% if object %}
<form action="{% url 'note:update' object.pk %}" method="post" accept-charset="utf-8">
{% else %}
<form action="{% url 'note:create' %}" method="post" accept-charset="utf-8">
{% endif %}
And in the index, add a link to edit the note in an appropriate place.
<a href="{% url 'note:update' note.id %}">Edit</a>