tags:

views:

157

answers:

3

In trying to save as much time as possible in my development and make as many of my apps as reusable as possible, I have run into a bit of a roadblock. In one site I have a blog app and a news app, which are largely identical, and obviously it would be easier if I could make a single app and extend it where necessary, and then have it function as two separate apps with separate databases, etc.

To clarify, consider the following: hypothetically speaking, I would like to have a single, generic news_content app, containing all the relevant models, views, url structure and templatetags, which I could then include and extend where necessary as many times as I like into a single project.

It breaks down as follows:

news_content/
    templatetags/
        __init__.py
        news_content.py
    __init__.py
    models.py (defines generic models - news_item, category, etc.)
    views.py (generic views for news, archiving, etc.)
    urls.py
    admin.py

Is there a way to include this app multiple times in a project under various names? I feel like it should be obvious and I'm just not thinking clearly about it. Does anybody have any experience with this?

I'd appreciate any advice people can give. Thank you.

+1  A: 

In this case you could create the common piece of code as a Python module instead of a whole new application.

Then for each instance you would like to use it, create an app and import the bits from that module.

Andre Miller
+2  A: 

What's the actual difference between blogs and news? Perhaps that difference ought to be part of the blog/news app and you include it just once.

If you have a blog page with blog entries and a news page with news entries and the only difference is a field in the database (kind_of_item = "blog" vs. kind_of_item = "news") then perhaps have you have this.

urls.py

(r'^/(?P<kind>blog)/$', 'view.stuff'),
(r'^/(?P<kind>news)/$', 'view.stuff'),

views.py

def stuff( request, kind ):
    content= news_blog.objects.filter( kind=kind )
    return render_to_response( kind+"_page", { 'content': content } )

Perhaps you don't need the same app twice, but need to extend the app to handle both use cases.

S.Lott
This is a useful suggestion, but is something I would be hesitant to include, simply because the sites I produce are for clients, and there are likely to be instances where they will require a blog, but not news. Unless, of course, it is possible to handle this app as two separate instances in the admin tool. I just want to make it as straightforward as possible for the end-user.
Gary Chambers
If you want the admin to look different, AFAIK you'll have to fix the models to have different kinds of names. This would make them copy-and-paste clones of each other with different names for the model elements.
S.Lott
+1  A: 

I'm not 100% sure I understand your question, so I'm going to list my understanding, and let me know if it is different from yours.

  1. You want to have a "news" and a "blog" section of your website with identical functionality.
  2. You want to have "news" and "blog" entries stored separately in the database so they don't end up intermingling.

If this is the case, I'd suggest making an API to your views. Something like this:

views.py:

def view_article(request, article_slug,
        template_name='view_article.html',
        form_class=CommentForm,
        model_class=NewsArticle,
        success_url=None,
        ):

urls.py:

(r'^news/(?P<article_slug>[-\w]+)/$', 'view_article', {}, "view_news_article"),
(r'^blog/(?P<article_slug>[-\w]+)/$', 'view_article', {'model_class': BlogArticle}, "view_blog_article"),

This makes your app highly reusable by offering the ability to override the template, form, model, and success_url straight from urls.py.

Jack M.