wrapper views for add_object or edit_object

This commit is contained in:
Sanj 2012-07-07 13:32:33 +05:30
parent 1e06d655ad
commit f495094eba
2 changed files with 42 additions and 0 deletions

View File

@ -6,5 +6,7 @@ urlpatterns = patterns('',
# (r'^get_details$', views.get_details),
# (r'^get_tab$', views.get_tab),
#(r'^static/(?P<module_slug>.*)/$', views.render_object),
(r'^(?P<module_slug>.*?)/(?P<tab_slug>.*?)/add', views.add_object),
(r'^(?P<module_slug>.*?)/(?P<tab_slug>.*?)/(?P<object_id>d+)/edit', views.edit_object),
(r'^(?P<module_slug>.*)/$', views.render_object),
)

View File

@ -3,6 +3,46 @@ from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from ox.django.shortcuts import render_to_json_response
import re
from django.http import HttpResponseRedirect
def add_object(request, module_slug, tab_slug):
tab = get_object_or_404(ModuleTab, slug=tab_slug)
model_class = tab.model_class()
add_form_name = model_class.main_form
form = model_class.get_forms()[add_form_name]
if request.POST:
f = form(request.POST, request.FILES)
if f.is_valid():
f.save()
return HttpResponseRedirect('/') #TODO: get the saved object and redirect to .get_absolute_url()
else:
f = form()
context = RequestContext(request, {
'form': f,
})
return render_to_response("add_form.html", context)
def edit_object(request, module_slug, tab_slug, object_id):
tab = get_object_or_404(ModuleTab, slug=tab_slug)
model_class = tab.model_class()
obj = get_object_or_404(
add_form_name = model_class.main_form
form = model_class.get_forms()[add_form_name]
if request.POST:
f = form(request.POST, request.FILES, instance=obj)
if f.is_valid():
f.save()
return HttpResponseRedirect(obj.get_absolute_url())
else:
f = form(instance=obj)
context = RequestContext(request, {
'form': f,
'object': obj
})
return render_to_response("edit_form.html", context)
'''