Django for Designers/Whats next: Difference between revisions

POST to index view
imported>Paulproteus
imported>Paulproteus
(POST to index view)
 
(7 intermediate revisions by 2 users not shown)
Line 17:
{% endif %}
</div>
{% if request.user.is_authenticated %}
{% 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 %}
<div class="actions"><form method="POST" action="{% url bookmarks.views.delete bookmark.id %}>
{% csrf_token %}
<input type="submit" value="Delete">
</form></div>{% endif %}
</source>
 
Line 49 ⟶ 53:
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.
 
Note that, at the moment, any user can delete any other user's bookmarks. If you want things to work differently, you'll need to add a check to the ''delete'' function in ''views.py''.
Updating
 
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].)
* there is an "update" link
 
* this goes to a bookmark.html-like page, but with the form fields
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.
* When you submit that, it saves, and redirects you to /
 
<source lang="html4strict">
{% block bookmark_widget %}
{% if request.user %}
<div id="new-bookmark-widget">
<form method="post">
{% csrf_token %}
<h3>Bookmark</h3>
{{ form.as_p }}
<p><button id="new-bookmark-submit">Submit</button>Submit</button>
</form>
</div>
{% endif %}
{% endblock %}
</source>
 
Within that fragment, make ''one'' change. Change this:
 
<source lang="html4strict">
<form method="post">
</source>
 
to this:
 
<source lang="html4strict">
<form method="post" action="{% url bookmarks.views.index %}">
</source>
 
That way, it POSTs to the index view.
 
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:
 
<source lang="html4strict">
{% 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:
 
<source lang="bash">
# 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 ===
 
...needs to be written-->
 
=== Handling user-uploaded media ===
Anonymous user