Skip to content

Commit

Permalink
Added basic form handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chhabrakadabra committed Apr 3, 2013
1 parent db19359 commit 1510a4c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
Binary file modified djangotodo/djangotodo.db
Binary file not shown.
5 changes: 5 additions & 0 deletions djangotodo/templates/todolist/list-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<li>{{task}}</li>
{% endfor %}
</ul>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Create!">
</form>
</div> <!-- /container -->

<!-- Le javascript
Expand Down
6 changes: 6 additions & 0 deletions djangotodo/todolist/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.forms import ModelForm
from models import Task

class TaskForm(ModelForm):
class Meta:
model = Task
19 changes: 17 additions & 2 deletions djangotodo/todolist/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
from django.shortcuts import render
from todolist.models import Task
from forms import TaskForm
from django.core.exceptions import PermissionDenied

def list(request):
return render(request, 'todolist/list-view.html', {
'tasks': Task.objects.all()
if request.method == "GET":
form = TaskForm()
elif request.method == "POST":
form = TaskForm(request.POST)
if form.is_valid():
form.save()
# make sure to return use a clean form for the resulting page
form = TaskForm()

else:
raise PermissionDenied('Only GET and POST methods are allowed')

return render(request, 'todolist/list-view.html', {
'tasks': Task.objects.all(),
'form': form
})

0 comments on commit 1510a4c

Please sign in to comment.