0

I am now working on a big django project.

Huge topicportal with many topiccenters and book and other media profiles and author profiles and normal user profiles who can also have author profiles..

Now what i do is:

One django app, templates folder as usual and now number of templates is becoming huge as i create templates ..

I have never experienced to have multiple apps in one project..

Can someone please tell me if thats what i am doing is ok or if i should go for multiple apps ?

Or if i put in different way: when does it make sense to have multiple apps in one project?

Thanks in advance

doniyor
  • 36,596
  • 57
  • 175
  • 260

2 Answers2

2

Django is made to thrive on having multiple apps. 1 App for user signup, 1 app for profiles, 1 app for comments, 1 app for blog postings, etc. etc.

Doing everything in one app will, as you pointed out, quickly lead to clutter. The best method with Django is to create many very specific applications. This also allows you to have more apps that you can reuse in other projects if you so wish.

There are dozens of possible "structures" for a Django project. I'd go for the most modular approach. And for every piece of functionality just ask yourself: Does this belong directly to another app? If the answer is no you're most likely better of creating a separate app for it.

E.g the same blog postings app can be used on multiple websites and you'd only have to change the template to make it fit the new layout/design of the page. All the logic should be mostly the same. And the logic of a blog post should not be linked to the logic of viewing a profile or something.

Example: for a comment app the model could look like this:

from django.db import models

from profile.models import Profile
from blog.models import Blog

class Comment(models.Model):
    user = models.ForeignKey(Profile, related_name='comments')
    blog = models.ForeignKey(Blog, related_name='comments')
    message = models.CharField(max_length=200)
EWit
  • 1,954
  • 13
  • 22
  • 19
  • thank you, very nice guidance. I was wondering because all models are in relation, dont i destroy the relation if i separate them into multiple apps? – doniyor Dec 17 '13 at 13:55
  • Well you can import the model from a different app just as easily as you'd import it from the same app. I'll update the answer with quick example. – EWit Dec 17 '13 at 13:58
  • You can create `ForeignKey`s between models of different apps. Just be careful not to create cycles (e.g. app A depends on B, which depends on A, ...); those not only create an import loop if you are not careful, they are also often a sign of over-complicated schemas... – sk1p Dec 17 '13 at 13:58
  • @EWit, dude, this is awesome. thank you so much, you gave a light now, i was really in deep confustion till now – doniyor Dec 17 '13 at 14:05
  • circuit imports between models are common. – iMom0 Dec 17 '13 at 14:14
  • I just wanted to ask you again.. can I also make separate "templates" folder for each app? would that work? i mean, is it good way of handling templates? – doniyor Dec 18 '13 at 12:45
  • Each app can/should have it's own templates folder. This folder is also automatically parsed by the Django system if the app is under INSTALLED_APPS. – EWit Dec 18 '13 at 12:48
  • so, i will list all template folders in TEMPLATE_DIRS, and thats it, right? – doniyor Dec 18 '13 at 13:05
1

You can have core apps which don't depend on others apps (most likely it will be something like)

  • core app with your core template views and models (
  • accounts with authentication templates and models
  • tools app with useful stuff

and "Application" apps which use the above

  • topiccenter1 - specific templates, views and models
  • topiccenter2 - specific templates, views and models

A nice structure to start with is a template from "Two Scoops of Django" https://github.com/twoscoops/django-twoscoops-project

"thank you, very nice guidance. I was wondering because all models are in relation, dont i destroy the relation if i separate them into multiple apps? "

You should be able to divide the models across apps - read:

Define ManyToMany relation in another application in Django

Django relationships between apps: How to keep apps separate?

Community
  • 1
  • 1
fragles
  • 680
  • 6
  • 19