Django for Designers/Whats next: Difference between revisions

→‎Updating and deleting bookmarks (the last two parts of CRUD): Delete should work; needs a fact-check or play-test
imported>Paulproteus
imported>Paulproteus
(→‎Updating and deleting bookmarks (the last two parts of CRUD): Delete should work; needs a fact-check or play-test)
Line 57:
To be able to '''update''' bookmarks, we will need to create add an ''update'' link, make it go to a page where bookmarks can be edited, and then let that page be saved. (If we wanted to make this slicker, the first step would be to put the edit page in a form using e.g. [http://jqueryui.com/resources/demos/dialog/modal-form.html jQuery UI].)
 
We already have a form that lets us enter bookmarks, and we can re-use it as the editing area. As a reminder, open ''index.html'' and notice the following fragment:. You don't have to make any changes.
 
<source lang="html4strict">
Line 74:
</source>
 
We will want the same widget in our new page. So we will extend ''index.html'' rather than extending the base. Create a new file called ''edit.html'' with the following contents:
...
fixme
 
<source lang="html4strict">
Now, let's create a template for the editing interface. Create a new file, ''edit.html'', as a sibling of ''index.html'', and give it the following contents:
{% extends 'index.html' %}
 
{% block subheader %}Edit bookmark{% endblock %}
 
{% block content %}{% endblock %}
</source>
 
This way, we empty out the content section, but keep the rest of the page layout intact. (It would probably be even better to extract the bookmark_widget to a separate block, and then have edit.html extend base.html instead of index. But hey, this works!)
 
Make sure to inform git you'll want that file to be part of the next commit:
* there is an "update" link
 
* this goes to a bookmark.html-like page, but with the form fields
<source lang="bash">
* When you submit that, it saves, and redirects you to /
# in django-for-designers/myproject
$ git add bookmarks/templates/edit.html
</source>
 
We'll need a view to render that form and to accept changes. So, open up ''views.py'' and add a new function to the end:
 
<source lang="python">
def edit(request, bookmark_id):
b = get_object_or_404(Bookmark, pk=int(bookmark_id))
context = {
'form': BookmarkForm(instance=b),
}
return render(request, 'edit.html', context)
</source>
 
In this snippet, we grab a bookmark by ID number instantiate a Django form based on, and pass that to the template.
 
Intriguingly, we do not need any special code to handle the submitted form. If it gets submitted to the index view, the existing machinery will handle it properly!
 
We still need to inform ''urls.py'' about the new view. To do that, open ''urls.py'' and add this line as the final line in the urlpatterns sequence:
 
<source lang="python">
url(r'^edit/(\d+)/$', 'bookmarks.views.edit'),
</source>
 
We do also need to change the base template so that it links to our edit functionality. Open ''base.html'' and find the following:
 
<source lang="html4strict">
{% if request.user.is_authenticated %}
<div class="actions"><form method="POST" action="{% url bookmarks.views.delete bookmark.id %}>
{% csrf_token %}
<input type="submit" value="Delete">
</form></div>{% endif %}
</source>
 
and replace it with:
 
<source lang="html4strict">
{% if request.user.is_authenticated %}
<div class="actions"><form method="POST" action="{% url bookmarks.views.delete bookmark.id %}>
{% csrf_token %}
<input type="submit" value="Delete">
</form> | <a href="{% url bookmarks.views.edit bookmark.id %}">edit</a></div>{% endif %}
</source>
 
Congratulations! You can now edit your bookmarks.
 
You'll do well to ''commit'' at this point.
 
<source lang="bash">
# in django-for-designers/myproject
$ git commit -a -m 'Update and delete functionality'
</source>
 
=== Enabling the built-in admin ===
Anonymous user