Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ed4f3e

Browse files
Add form processing code
1 parent 2e22fde commit 8ed4f3e

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

‎units/django/5/form-processing.md‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,96 @@ todoapp=# SELECT * FROM todos_todo;
4848
(6 rows)
4949
```
5050

51+
## Forms
52+
Now that we know how to create or manipulate our data. We need to work on our forms so as to allow our users to create and update data using them.
53+
54+
### Create Todo Form
55+
We already have setup a page with the create todo form previously now we're going to make it fully functional. Before doing that we need to add a new url to which the form will be submitted to.
56+
57+
#### Add a new url
58+
So, we'll start by adding a new url `save` to our `todos/urls.py` file, which should look like this:
59+
60+
```python
61+
from django.conf.urls import url
62+
from todos import views
63+
64+
urlpatterns = [
65+
url(r'^$', views.index, name='index'),
66+
url(r'^create$', views.create, name='create'),
67+
url(r'^save$', views.save, name='save')
68+
]
69+
```
70+
#### Add a new view function
71+
Now we'll also need to add `views.save` function that we're referenced above, which should look like this:
72+
73+
```python
74+
from django.shortcuts import render, redirect
75+
76+
def save(request):
77+
# TODO: Logic to save the todo item here.
78+
79+
return redirect('index')
80+
```
81+
82+
Currently, the above code does nothing but just redirect back to `index` page url. But here we need to get the data received from the form, save it and then finally redirect to index.
83+
84+
### Saving the form data
85+
Let's update the `save` function we've just created to receive data from the form and save it using our `Todo` model. The code should look like this:
86+
87+
```python
88+
from django.shortcuts import render, redirect
89+
from django.utils import timezone
90+
from todos.models import Todo
91+
92+
def save(request):
93+
# Get the form data from the request.
94+
title = request.POST.get('title')
95+
description = request.POST.get('description')
96+
97+
# Create a new todo item with the data.
98+
Todo.objects.create(
99+
title=title,
100+
description=description,
101+
created_at=timezone.now()
102+
)
103+
104+
# Redirect back to index page
105+
return redirect('index')
106+
107+
```
108+
109+
### Updating the HTML form
110+
We now need to update our HTML form to submit the data properly to the server like what we've expected in the above code.
111+
112+
We'll need to ensure the following things:
113+
1. Form should be submitted to url `save` (`name='save'`).
114+
2. Form should be submitted using method `POST`
115+
3. Two form fields are required to be sent `title` and `description` for the todo item.
116+
4. And CSRF token should be sent along with the form too.
117+
118+
After making these changes to our plain html form it should look like this:
119+
```html
120+
{% extends 'base.html' %}
121+
{% block content%}
122+
<h3>Add Todo</h3>
123+
<div class="todo-form">
124+
<form method="post" action="{% url 'save' %}">
125+
{% csrf_token %}
126+
<div class="form-group">
127+
<label for="input-todo-title">Title</label>
128+
<input type="text" name="title" class="form-control" id="input-todo-title" placeholder="What do you want to do?">
129+
</div>
130+
<div class="form-group">
131+
<label for="description">Description</label>
132+
<textarea name="description" class="form-control" id="description" placeholder="Description"></textarea>
133+
</div>
134+
135+
<button type="submit" class="btn btn-primary">Save</button>
136+
</form>
137+
</div>
138+
{% endblock %}
139+
```
140+
51141
## Source Code
52142
Check the full source code [here](https://github.com/kabirbaidhya/django-todoapp/tree/master).
53143

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /