TODO バージョン2
ログイン機能をつけた。
ログインからデータの追加、更新の流れが理解できてきた感じ。
ブログづくりにとりかかろうかな
File List (作成分のみ)
+ src + templates + registration - login.html - logged_out.html + task - task_form.html - task_list.html + todo + task - models.py - vies.py - urls.py
urls.py
from django.conf.urls.defaults import * from todo.task.models import * urlpatterns = patterns('django.views.generic.list_detail', (r'^$', 'object_list', dict(queryset=Task.objects.all(), allow_empty=True)), (r'^noend/$', 'object_list', dict(queryset=Task.objects.filter(status__exact=0))), ) urlpatterns += patterns('django.views.generic.create_update', (r'^add/$','create_object', dict(model=Task, post_save_redirect='/',login_required=True)), ) urlpatterns += patterns('django.contrib.auth.views', (r'^accounts/login/$','login'), (r'^logout/$','logout'), ) urlpatterns += patterns('', (r'^task_end/(?P<task_id>\d+)/$', 'todo.task.views.update_task_status'), (r'^admin/', include('django.contrib.admin.urls')), )
models.py
from django.db import models # Create your models here. class Task(models.Model): task_id = models.AutoField(primary_key=True) contents = models.CharField(_('Contents'), maxlength='50') status = models.BooleanField(_('Status')) class Admin: pass
views.py
# Create your views here. from django.shortcuts import get_object_or_404, render_to_response from django.http import HttpResponseRedirect from todo.task.models import Task def update_task_status(request, task_id): if not request.user.is_authenticated(): return HttpResponseRedirect('/accounts/login/?next=%s' % request.path) else: task = get_object_or_404(Task, pk=task_id) task.status = True task.save() return HttpResponseRedirect ('/')
task_form.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional/EN"> <html> <head> <title>Untitled</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <body> <p> <a href="/">タスク一覧</a> </p> <h1>Task Add</h1> <form action="" method="post" enctype="multipart/form-data"> <p><label for="id_contents">タスク名:</label> {{ form.contents }}</p> <input name="" type="submit" class="submit" value="登録" /> </form> <BR> </body> </html>
task_list.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>ToDo</title> </head> <body> <h1>Task List</h1> <a href="/">全表示</a> : <a href="/noend">未完了表示</a> <table width="100%" border="1"> <tr> <th nowrap="nowrap">No.</th> <th nowrap="nowrap">タスク</th> <th nowrap="nowrap">状態</th> <th nowrap="nowrap">完了</th> </tr> {% for task in object_list %} <tr> <td>{{ task.task_id }}</td> <td>{{ task.contents }}</td> {% if task.status %} <td>完了</td> <td> </td> {% else %} <td>未完</td> {% if user.is_authenticated %} <td><a href="/task_end/{{ task.task_id }}">終わりました</a></td> {% else %} <td> </td> {% endif %} {% endif %} </tr> {% endfor %} </table> <p> {% if user.is_authenticated %} Welcome, {{ user.username }}. Thanks for logging in.<br> <a href="/add">新規タスク</a><br> <a href="/logout">ログアウト</a><br> {% else %} <a href="/accounts/login">ログイン</a><br> {% endif %} </p> </body> </html>
login.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional/EN"> <html> <head> <title>Login</title> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <meta name="generator" content="PyScripter"> </head> <body> {% if form.has_errors %} <p><font color="red">Your username and password didn't match. Please try again.</font></p> {% endif %} <form method="post" action="."> <table> <tr><td><label for="id_username">Username:</label></td><td>{{ form.username }}</td></tr> <tr><td><label for="id_password">Password:</label></td><td>{{ form.password }}</td></tr> </table> <input type="submit" value="login" /> {% ifequal next "" %} <input type="hidden" name="next" value="/" /> {% else %} <input type="hidden" name="next" value="{{ next }}" /> {% endifequal %} </form> </body> </html>
logged_out.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional/EN"> <html> <head> <title>Login</title> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <meta name="generator" content="PyScripter"> </head> <body> ログアウトしました。<br> <a href="/accounts/login">再度ログインする。</a> </body> </html>