Forest Bond wrote:
> On Sun, Jul 15, 2007 at 02:59:57PM -0500, Carl Karsten wrote:
>> How can I use a method as a view? (so that I can subclass and extend later.)
>>
>> foo works, the other 2 give errors:
>>
>> #  msg/urls.py
>> from django.conf.urls.defaults import *
>> urlpatterns = patterns('msg.views',
>>      (r'^detail/(?P<object_id>[-\w]+)/$', 'foo'),
>>      (r'^detail/x/(?P<object_id>[-\w]+)/$', 'MsgViews.message_detail'),
>>      (r'^detail/y/(?P<object_id>[-\w]+)/$', MsgViews.message_detail),
>> )
> 
> Try this:
> 
> --------------------------------------------------------------------------------
> from django.conf.urls.defaults import *
> 
> msg_views = MsgViews()
> 
> urlpatterns = patterns('msg.views',
>   (r'^detail/(?P<object_id>[-\w]+)/$', 'foo'),
>   (r'^detail/x/(?P<object_id>[-\w]+)/$', msg_views.message_detail),
>   (r'^detail/y/(?P<object_id>[-\w]+)/$', msg_views.message_detail),
> )

That works,
Thanks.

> --------------------------------------------------------------------------------
> 
> Alternatively, you can make message_detail a static method:
> 
> --------------------------------------------------------------------------------
> class MsgViews(object):
> 
>     @static_method
>     def message_detail(request, object_id=None):
>         m=get_object_or_404(Message, pk=object_id)
>         return render_to_response('message_detail.html', {'message': m})
> --------------------------------------------------------------------------------
> 
> I'm not even sure if this sort of thing is possible, but it might be:
> 
> --------------------------------------------------------------------------------
> class MsgViews(object):
> 
>     @static_method
>     def __call__(request, object_id=None):
>         m=get_object_or_404(Message, pk=object_id)
>         return render_to_response('message_detail.html', {'message': m})
> --------------------------------------------------------------------------------
> 

error:  name 'static_method' is not defined

> The only reason I can think of for doing that is that you wouldn't have to
> import MsgViews in your urls.py, you could just specify a string.
> 
> Now, after all that, I'm still a little confused as to why you would want to 
> do
> all of this?  You say so that you can extend your view functions, but classes
> are extensible because their methods can be replaced, and you already _can_
> replace plain view functions...

Classes also can call self.other method, and the code they are extending.  Hmm, 
now that I think about it, you may have a point.  I'll leave it as is for now, 
but if I have any more problems I will probably go back to module.function.

My overall goal is to figure out how to make components that can be moved 
between django apps/sites.  I kinda have models working, but im gonna burn for 
using the same name for both parent and subclass.

# msg/model.py
from django.db import models
from django.contrib.auth.models import User

class Message(models.Model):
     to = models.ForeignKey(User, related_name = "messages_received")
     sender = models.ForeignKey(User, related_name = "messages_sent")
     subject = models.CharField(maxlength=50)
     sent = models.DateTimeField(auto_now_add=True)
     recieved = models.DateTimeField(null=True)
     read = models.BooleanField(default=False)
     body = models.TextField()
     def __str__(self):
         return self.subject
#    class Admin:
#        list_display = ('sent', 'to', 'sender', 'subject')
     class Meta:
         db_table = 'message'


# core/models.py
import msg.models

class Message(msg.models.Message):
     status = models.CharField(maxlength=1, blank=True)
     class Admin:
         list_display = ('sent', 'to', 'sender', 'subject')

Carl K

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to