Anonymous avatar Anonymous committed cdfda60

La vue générique CreateView

Comments (0)

Files changed (3)

personal_calendar/templates/personal_calendar/evenement_form.html

+{% extends "base.html" %}
+
+{% block titre %}
+    | Créer un évenements
+{% endblock titre %} 
+{% block content %}
+<div class="well">
+<h1>{% if form.initial.nom %} {{form.initial.nom}} {%else%}Nouvel évennement {%endif%}</h1>
+<form action="" method="POST" class="form-horizontal">
+{% csrf_token  %}
+{{form.as_p}}
+<input type="submit" value="{% if form.initial.nom %}Modifier{%else%}Créer{%endif%}" />
+</form>
+</div>
+{% endblock content %}

personal_calendar/urls.py

 from django.conf.urls.defaults import patterns, include, url
-from views import create, delete, details, update, delete_participant, Evenement_Liste, Evenement_Detail
+from models import Evenement
+from forms import EventForm
+from views import delete, update, delete_participant, Evenement_Liste, Evenement_Detail
+from django.views.generic.edit import CreateView
+
 
 urlpatterns = patterns('',
-    url(r'^create/$', create),
+    url(r'^create/$', CreateView.as_view(model=Evenement, form_class=EventForm)),
     url(r'^(?P<pk>\d+)/detail/$', Evenement_Detail.as_view(), name='details'),
     url(r'^liste/$', Evenement_Liste.as_view(paginate_by=1)),
     url(r'^liste/(?P<champ>[\w-]+)/(?P<terme>[\w-]+)/$',Evenement_Liste.as_view(

personal_calendar/views.py

 from django.views.generic.detail import DetailView
 import datetime
 
-def create(request):
-    if request.method == "POST":
-        form = EventForm(request.POST)
-        if form.is_valid():
-            event = form.save()
-            return HttpResponseRedirect('/agenda/%i/details/'%event.pk)
-    else:
-        form = EventForm()
-    return render(request,'personal_calendar/event/create.html',{'form':form})
 
 class Evenement_Detail(DetailView):
     model = Evenement
                         'event':form.instance.evenement,
                         'form':form})
 
-def details(request, id):
-    event = Evenement.objects.get(pk = id)
-    if request.method == "POST":
-        form = Evenement_ParticipantForm(request.POST)
-        if form.is_valid():
-            form.save()
-            if request.is_ajax():
-                delete_form = render_to_string(
-                    "personal_calendar/blocks/delete_form.html",
-                    {'delete_url': form.instance.delete_url(),
-                     },
-                    RequestContext(request)
-                    )
-                data = {'participant': form.instance.participant.username,
-                        'get_status_display': form.instance.get_status_display(),
-                        'delete_form': delete_form}
-                return HttpResponse(
-                    json.dumps(data),
-                    mimetype="application/json"
-                    )
-            return HttpResponseRedirect('/agenda/%s/details/' % id)
-    else:
-        form = Evenement_ParticipantForm(initial = {'evenement':event})
-    if request.is_ajax():
-        return render_to_response(
-            'personal_calendar/blocks/participant_form.html',
-            {'event':event,
-             'form': form})
-
-    return render(request,'personal_calendar/event/details.html',{
-            'event':event,
-            'form':form})
-
 def delete_participant(request, id, participant):
     if request.method == "POST":
         evenement = Evenement.objects.get(pk = id)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.