Reusing a Django Application in the Same Project

When trying to save as much time as possible in my development and make as many of my applications as possible as possible, I ran into a small hurdle. On one site, I have a blog application and a news application that is pretty much identical, and it would obviously be easier if I could make one application and expand it where necessary, and then use it as two separate applications with separate databases, etc.

To clarify, consider the following: hypothetically speaking, I would like to have one universal news_content application that contains all the relevant models, views, url structure and templatetags, which I could then include and expand where necessary, as many times as I like, in one project.

It is broken 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 application several times in a project under different names? I feel that this should be obvious, and I just don't think about it clearly. Does anyone have any experience with this?

I would be grateful for any advice that people can give. Thanks.

+4
source share
3 answers

What is the actual difference between blogs and news? Perhaps this difference should be part of the blog / news app, and you only enable it once.

If you have a blog blog page and a news news page, and the only difference is the field in the database (kind_of_item = "blog" and kind_of_item = "news"), maybe 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 } ) 

You may not need the same application twice, but you need to expand the application to handle both use cases.

+3
source

In this case, you can create the general part of the code as a Python module instead of a whole new application.

Then for each instance that you would like to use, create an application and import a bit from this module.

+2
source

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

  • You want to have a β€œnews” and β€œblog” section of your site with the same functionality.
  • You want to have news and blog entries that are stored separately in the database so they don't mix.

If so, I would suggest making an API for your views. Something like that:

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 application reusable, offering the ability to undo template , form , model and success_url directly from urls.py

+1
source

All Articles