wrapper views for add_object or edit_object
This commit is contained in:
parent
1e06d655ad
commit
f495094eba
|
@ -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),
|
||||
)
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
||||
'''
|
||||
|
|
Loading…
Reference in New Issue
Block a user