Commits

Kirill Nikolenko committed e4399b7

add edit

  • Participants
  • Parent commits b542fb4

Comments (0)

Files changed (8)

File students_crud/managment/commands/getrs.py

-# -*- coding: utf-8 -*-
-print 'fff'
-from django.core.management.base import AppCommand
-from optparse import make_option
-
-class Command(AppCommand):
-    option_list = AppCommand.option_list + (
-        make_option('--count', action='store_true', dest='count', default= False,
-            help='Add object count information' ),
-    )
-    help = 'Prints model names for given application and optional object count.'
-    args = '[appname ...]'
-
-    requires_model_validation = True
-
-    def handle_app(self, app, **options):
-        from django.db.models import get_models
-
-        lines = []
-
-        for model in get_models( app ):
-            lines.append( "[%s]" % model.__name__ + ( options["count"] and " - %s objects" % model._default_manager.count() or "" ) )
-
-        return "\n".join( lines )

File students_crud/urls.py

             model=Group,
             template_name='detail.html')),
     url(r'^edit/$', 'students_crud.views.edit_groups'),
-    url(r'(?P<group_id>\d+)/edit/', 'students_crud.views.edit_students'),
-    
+    url(r'(?P<group_id>\d+)/edit/', 'students_crud.views.edit_group'),
+    url(r'(?P<group_id>\d+)/(?P<st_id>\d+)/', 
+        'students_crud.views.edit_student'),
+
 )

File students_crud/views.py

 # -*- coding: utf-8 -*-
 from django.template import RequestContext
-from django.http import HttpResponseRedirect
+from django.http import HttpResponseRedirect, HttpResponse
 from django.shortcuts import render_to_response
 from django.core.urlresolvers import reverse
 from django.contrib.auth.decorators import login_required
         form = GroupForm(request.POST)
         if form.is_valid():
             form.save()
-        return HttpResponseRedirect(reverse(edit_groups))
+        return HttpResponseRedirect('/students/')
     else:
         form = GroupForm()
     return render_to_response('edit.html', {'form': form}, context_instance=RequestContext(request))
 
+
 @login_required
-def edit_students(request, group_id):
+def edit_group(request, group_id):
     if request.method == "POST":
+        if request.POST.get('del', '') == u'Удалить':
+            gr = Group.objects.get(pk=group_id)
+            gr.delete()
+            return HttpResponseRedirect('/students/')
+
+        form = GroupForm(request.POST)
+        if form.is_valid():
+            if request.POST['act'] == 'add':
+                form.save()
+            else:
+                gr = Group.objects.get(pk=group_id)
+                form = GroupForm(request.POST, instance=gr)
+            form.save()
+        return HttpResponseRedirect('/students/')
+    else:
+        gr = Group.objects.get(pk=group_id)
+        form = GroupForm(instance=gr)
+
+    return render_to_response('edit.html', {'form': form}, context_instance=RequestContext(request))
+
+
+@login_required
+def edit_student(request, group_id, st_id):
+    if request.method == "POST":
+        if request.POST.get('del', '') == u'Удалить':
+            st = Student.objects.get(pk=st_id)
+            st.delete()
+            
         form = StudentForm(request.POST)
         if form.is_valid():
-            #form.save()
-            pass
-        return HttpResponseRedirect(reverse(edit_groups))
+            if request.POST['act'] == 'add':
+                    form.save()
+            else:
+                st = Student.objects.get(pk=st_id)
+                form = StudentForm(request.POST, instance=st)
+            form.save()
+        return HttpResponseRedirect('/students/'+group_id+'/')
     else:
-        #gr = Group.objects.get(pk=group_id)
-        form = StudentForm()
-    return render_to_response('edit.html', {'form': form}, context_instance=RequestContext(request))
+        st = Student.objects.get(pk=st_id)
+        form = StudentForm(instance=st)
+    
+    return render_to_response('edit.html', {'form': form}, context_instance=RequestContext(request))

File templates/base.html

 </head>
 <body>
 <div id="header">
-    <p id="title">Заголовок</p>
+    <p id="title"><a href="/students/">Главная</a></p>
 </div>
 {% block content%}
 

File templates/detail.html

 {% for student in group.student_set.all %}
     
     <tr>
-        <td>{{ student.full_name }}</td> <td>{{ student.birthday }}</td> <td>{{ student.id_card }}</td> 
+        <td><a href={{student.id}}/>{{ student.full_name }}</a></td> <td>{{ student.birthday }}</td> <td>{{ student.id_card }}</td> 
     <tr>
     
 {% endfor %}
 </table>
-<p><a href="edit/">Редактировать</a><p>
+<p><a href="edit/">Редактировать группу</a><p>
 
 {% endblock %}

File templates/edit.html

 {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
 <form action="" method="POST">
 {% csrf_token %}
-    {{ form.as_table }}
-<input type="submit" value="Добавить" />
+    {{ form.as_ul }}
+<select name="act">
+    <option value="add">Добавить</option>
+    <option value="save">Сохранить</option>
+</select>
+<input type="submit" value="Выполнить" />
+</form>
+
+<form action="" method="POST">
+    {% csrf_token %}
+    <input type="submit" value="Удалить" name="del"/>
 </form>
 {% endblock %}

File templates/index.html

 
 {% endif %}
 
-<p><a href="edit/">Редактировать</a><p>
+<p><a href="edit/">Добавить группу</a><p>
 
 {% edit_link group %}
 {% endblock %}

File templates/templ.html

-{% load admredir %}
-<!DOCTYPE html> 
-<html>
-<head>
-    <title>База данных студентов</title>
-    <style type="text/css">
-          .sql_info {
-            font: 0.5em "Lucida Grande", "Trebuchet MS", Verdana, sans-serif;
-            color: grey;
-          }
-    </style>
-</head>
-<body>
-<div id="header">
-    <p id="title">Заголовок</p>
-</div>
-{% block content%}
-{% endblock %}
-<p></p>
-<div id="footer">
-    2012 ©
-</div>