Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newbie Django: Creating a project with several apps or all in one

Tags:

python

django

I am begginer in the Django world, I developed some "information sites" (nothing complicated) but this week my boss order me to make a migration of a big software that has 7 modules.

So I went to read the documentation page and search in google for how I could design this software using Django. I know that the every "module" can named as "app", so I create a new project and one app for every module (I dont know if it was right because the modules will not be public).

The problem is that now I don't know what is the next step.

All my apps can share data (every app has its owns models but sometimes one app has a model that was related to the models in other apps)?

Where do I write the code for the login process (I create a manageUsers app that was thinked to handle the registration, edit, share and validate profile of the current or new user ) and we can be able to share this session data accross the apps?

I need one more app for put the website information (like contact, about, pricing ...)? I use Python 2.7, Django 1.3, Memcached and Mysql 5.

If someone can help me or tell me where it may clarify these questions because most explains how to develop using only one app and in the IRC got no reply or else I must be write all the code in one app?

Best Regards

like image 854
rcsolis Avatar asked Dec 13 '22 04:12

rcsolis


2 Answers

A good place to start (dated, but worth reading; look at user comment bubbles too): http://www.djangobook.com/en/2.0/ . Chapter 1 - 10 are essential reading. You can pick-and-choose to read the remaining chapters, if desired.

Yes, all Django Apps can share data with one another. You make multiple Django Application's, housed under a single Django Project. The Project sets up a common database to use, and each Application creates Models which use said database. App1 can talk to App2 and vice-versa.

Django Project (one)  <----->>  (many) Django Application

Typically you separate Apps based on common function. User accounts get their own app (see Auth below). Blog postings get another. A Google Maps interface will get another. User subscriptions, another.

For user accounts and login, Django provides the Auth Module. You can have user accounts stored directly in Django, or configure it to talk to something else, like Active Directory. Auth works "pretty good" out of the box, though I personally customized mine a bit to allow 255-character email addresses as usernames (by default, it limits to 40 characters). Chapter 14 in the Django book might be a little easier to read than the official Auth docs. If you do use Auth, you don't have to make your own Django Application, since Auth already is one! You just install it in settings.py and you're golden.

Your Django structure will likely look something like this:

/Project/
  __init__.py
  manage.py
  settings.py
  urls.py

  App1/
    __init__.py
    forms.py
    models.py
    views.py
    templates/App1/
      template1.html
      template2.html

  App2/
    ...

App2 can access the data-models of App1 by doing: from Project.App1.models import someModel

like image 93
Dave Avatar answered May 15 '23 11:05

Dave


For me, rules are simple.

  1. If you need to copy-paste some code from one project to another - make an app for it
  2. If one of app's modules code is bigger than 1k lines and/or hard to maintain - look for something to move in separate app
  3. Group functionality into apps to minimize cross-linking between them

For interconnection you can use signals and sessions

like image 24
ilvar Avatar answered May 15 '23 13:05

ilvar