I would like tighten the link from URLs to instances of Models and
have something like:

import foo.FooModel

urlpatterns = patterns('',
    (r'foo/create/?$',                   'foo.FooModel.create'),
    (r'foo/(?P<foo_id>\d+)/?$',     'foo.FooModel.view'),
    (r'foo/(?P<foo_id>\d+)/edit$', 'foo.FooModel.edit'),
)

Basically I'd like some way that URL operations map directly to the
model methods, either class methods (for statics or for instance
creation), or to a method that is "in context" relative to a
particular instance somehow fetched from the URL parameters (in the
example here, via the <foo_id> pattern group).

I realize this isn't in typical Django style, but I'd be interested
any feedback.  The rough idea is outlined below.

Regards,

Ian

class FooModel(Model):
    @classmethod
    def create(cls, request):
        if request.method == "GET":
            blank = FooForm()
            result = render_to_response("foo_creation_form.html",
{'form': blank})
        elif request.method == "POST":
            completed = FooForm(request.POST, request.FILES)
            if completed.is_valid():
                foo = completed.save()
                result = HttpResponseRedirect(reverse
('module.FooModel.view', kwargs={'foo_id': foo.id}))
            else:
                result = render_to_response("foo_creation_form.html",
{'form': completed})
        return result

    @classmethod
    def view(cls, request, foo_id=None):
        foo = get_object_or_404(FooModel, id=foo_id)
        completed = FooForm(instance=foo)
        result = render_to_response("foo_view_form.html", {'form':
completed})
        return result

    @classmethod
    def edit(cls, request):
        if request.method == "GET":
            foo = get_object_or_404(FooModel, id=foo_id)
            completed = FooForm(instance=foo)
            result = render_to_response("foo_edit_form.html", {'form':
completed})
        elif request.method == "POST":
            completed = FooForm(request.POST, request.FILES)
            if completed.is_valid():
                foo = completed.save()
                result = HttpResponseRedirect(reverse
('module.FooModel.view', kwargs={'foo_id': foo.id}))
            else:
                result = render_to_response("foo_edit_form.html",
{'form': completed})
        return result

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to