8

Can I use the class method in views.py?

 2 years ago
source link: https://www.codesd.com/item/can-i-use-the-class-method-in-views-py.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Can I use the class method in views.py?

advertisements

Now my code is like:

def use_item(request):
    itemname = request.get('value')
    if itemname == 'item1':
        #do something
    if itemname == 'item2':
        #do something else

Can I do it in the following way?

views.py

class use_item():
    def use_item(self,request):
        itemname = request.get('value')
        use = getattr(self,itemname) # say itemname is 'item1'
        use()

    def item1(self,request):
        #do something

    def item2(self,request):
        #do something else

I've tried the second method but it seems that I was not doing it right. And the reason I want to do it in this way is that I hope to group the methods that they'd be more organized.

the actual code

#views.py
class use_item():
    def useitem(self,request):
        itemname = request.POST.get('value')
        use = getattr(self,itemname)
        use()
    def jelly(self,request,topic_id):
        t = topic.objects.get(id=topic_id)
        t.top = True
        t.time_topped = datetime.datetime.now()
        t.save()

#urls.py
    url(r'^use/item/(?P<topic_id>\d+)/$', 'use_item.use_item', name='use_item'),


If you want to have a better organization of your code, and reuse some code accross different views, instead of pasting it where you need, you may use the Django class based views:

# views.py
from django.views import View

class use_item(View):

    def get(self, request, *args, **kwargs):
        itemname = request.POST.get('value')
        use = getattr(self,itemname)
        use()

    def item1(self,request):
        #do something

    def item2(self,request):
        #do something else

# urls.py
from package.views import use_item

urlpatterns = [
    # [...]
    url(r'^use/item/(?P<topic_id>\d+)/$', use_item.as_view(), name='use_item'),
    # [...]
]

But, if at some point you need to call item1() or item2() from another view (is it the reason you mentioned the other view jelly ?), you will see that it is not possible.

One solution could be moving the common methods in another class and make sure your views inherit it. This is often called mixins in django world.

# views.py
from django.views import View

class ItemsRelatedLMixin:
    def item1(self, request):
        #do something

    def item2(self, request):
        #do something else

class use_item(ItemsRelatedLMixin, View):

    def get(self, request, *args, **kwargs):
        itemname = request.POST.get('value')
        use = getattr(self,itemname)
        use()

class jelly(ItemsRelatedLMixin, View):

    def get(self, request, topic_id):
        t = topic.objects.get(id=topic_id)
        t.top = True
        t.time_topped = datetime.datetime.now()
        t.save()

Now, your views jelly and use_item call the common methods. Of course you can define new methods in a view to make them available only from that view. You could also create class members to store values you use often etc. Keep in mind that each request received by Django will trigger creation of a new instance of your view class (you can't keep data stored between 2 requests in class members).


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK