Wednesday, September 11, 2013

Re: Best practice: Models.py vs View.py?

The models module is not limited to back-end data, it can include all kinds of models so your Page class can definitely be in it.

For the helpers, it really depends on your code. 
If they are simple enough and used only in your views, it may be OK to place them in views.py whereas if they are complex and/or used by other modules, it's a good idea to use a different module (e.g. helpers.py or utils.py).



On Wednesday, 11 September 2013 02:32:08 UTC+1, Storn White wrote:
I am developing my first relatively complex django project.  I have written some code and I'm not sure whether it belongs in an app's models.py or views.py file.

I have written a class and a few functions that have to do with routing content to views.  For example, all the different *types* of pages on my site are created as instances of a class name Page which represents a generic page that is then customized with templates that are specific to that type of page.  For example, I create an order page for catering orders with:

order/views.py:

from main.views import Page

def cater(request):
    """
    Displays the order/cater page.
    """
    page = Page(request,
                       menu_template='dn/menu/order.html',
                       submenu_template='order/menu/cater.html',
                       content_template='order/content/cater.html'
    )
    return page.render()

My question is this...    Does Page belong in views.py or models.py?

Page does not subclass django.db.models.Model and there is no corresponding back-end data.  However it is an object that models common site-wide elements of the site.  I have a few other helper functions as well that are used by the view functions, but that aren't directly called by urls.py.  What's the best practice for placing this stuff?

Thanks!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate