Jump to content

Django for Designers/Whats next: Difference between revisions

imported>Paulproteus
imported>Paulproteus
Line 3:
=== Updating and deleting bookmarks (the last two parts of CRUD) ===
 
Let's make it possible to delete a bookmark. The changes we will make to support that are to add a delete button next to each bookmark, add a a view that handles that delete button, and hook all that up to urls.py.
Deleting
 
To make the delete button appear, edit ''bookmark.html'' so that it has the following text, top to bottom. The only change we are making is the addition of a new div toward the bottom.
* If user is logged in, add a button for delete next to each bookmark
 
* That buttton, if clicked, POSTs to a new view, and that view then redirects back to the home page
<source lang="html4strict">
<li>
<a class="bookmark-link" href="{{ bookmark.url }}">{% if bookmark.title %}{{ bookmark.title }}{% else %}{{ bookmark.url }}{% endif %}</a>
<div class="metadata"><span class="author">Posted by {{ bookmark.author }}</span> | <span class="timestamp">{{ bookmark.timestamp|date:"Y-m-d" }}</span>
{% if bookmark.tag_set.all %}| <span class="tags">
{% for tag in bookmark.tag_set.all %}
<a href="{% url 'bookmarks.views.tag' tag.slug %}">{{ tag.slug }}</a></span>
{% endfor %}
{% endif %}
</div>
{% 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>
 
We added a very dense block there. Here is an explanation, piece-by-piece:
 
* It only renders if there is a user logged-in.
* It looks for a URL corresponding to a view (that we have not yet written) called bookmarks.delete.
* It passes the bookmark ID to that view as a URL parameter.
* It provides a CSRF token, and a submit button labeled ''Delete''.
 
To make that work, the next step to to take is to write our view. Open up ''bookmarks/views.py'' and add the following to the end of the file:
 
<source lang="python">
def delete(request, bookmark_id):
if request.method == 'POST':
b = get_object_or_404(Bookmark, pk=int(bookmark_id))
b.delete()
return redirect(index)
</source>
 
Namely, if the request is a POST, we try to find the corresponding bookmark and delete it. No matter what, we redirect back to the start.
 
Finally, we need to adjust urls.py to be aware of this view. To do that, open up ''urls.py'' in your editor, and add this line anywhere within the urlpatterns sequence (for example, underneath the final current URL pattern):
 
<source lang="python">
url(r'^delete/(\d+)/$', 'bookmarks.views.delete'),
</source>
 
(The (\d+) means, "any sequence of digits, 1 or longer, captured." Capturing it permits the variable to be passed to the Python view function.)
 
Save all of that, and now try loading up the home page. If you see a bookmark you want to delete, click the ''delete'' button. What should appear to happen is that the page reloads itself, and that bookmark is now gone.
 
Updating
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.