Multiple applications with django


Question

I am studying the Django framework, reading the documentation, but have not found references on how to create more than one application on the same project, where each application has multiple apps. For example, the application Blog, with apps Users, Comments, Posts, ..., implementation and Tasks with the apps Clients, Users, Attachments ... How should I do?

localhost
localhost/blog/ (posts, user, comment, ...)
localhost/tasks/ (task, user, attachment, ...)

<my project>
-- manage.py
-- <project name>
-- -- blog
-- -- -- posts
-- -- -- -- views.py
-- -- -- -- urls.py
-- -- -- -- models.py
...
-- -- tasks
-- -- -- attachments
-- -- -- -- views.py
-- -- -- -- urls.py
-- -- -- -- models.py
...
1
17
7/15/2012 2:06:37 AM

Accepted Answer

Each app will have these in their folder:

  • views.py
  • models.py
  • others optionals (admin.py...)

So, you have :

Project
-- manage.py
-- Project
-- -- views.py
-- -- models.py
-- -- others
-- -- APP1
-- -- -- views.py
-- -- -- models.py
-- -- -- others
-- -- APP2
-- -- -- views.py
-- -- -- models.py
-- -- -- others
-- -- APPX
-- -- -- views.py
-- -- -- models.py
-- -- -- others

The utility is the distinction between functionalities (call Notification.models.notification if you have a Notification app and a notification model in it).

A model is the architecture of an object. So you will call for example User.username or Task.name. Django creates automaticaly in the database the table.

Another tool of Django : you can use admin view (wich can edit, create, edit, objects that use your models) or change this interface (look that page)

If you want you can make an identification system, and create forms, you can use your own admin system.

WITH YOUR EXAMPLE:

<my project>
-- manage.py
-- <project name>
-- -- urls.py
-- -- models.py <--- User model exists in Django and here you can add informations to the defaut model it is in this file because I suppose it will be used in all your project.
-- -- views.py

-- -- blog    <--- It's an app.
-- -- -- -- views.py 
-- -- -- -- urls.py
-- -- -- -- models.py <--- Posts, Comments are models for the blog, so you explain them here.

-- -- tasks   <--- Another app.
-- -- -- attachments
-- -- -- -- views.py
-- -- -- -- urls.py
-- -- -- -- models.py  <--- Task model for exemple
16
3/19/2014 9:17:32 AM

Things you should understand about django:

  1. URL map has no bearing on the file system or the application code. This means that your URLs do not have a 1-to-1 relationship with your code. You can have multiple URLs pointing to same piece of code (pointing to the same view). This is different than PHP for example, where URLs map to the file system.

  2. Applications are not "widgets" or "portlets". Application is just a python module with some files already included (views.py, models.py and __init__.py); and you can have as many apps as you want. You can also have applications that are not accessible using a URL - they are just there to support other applications; and your applications do not have to have the same name as the URLs either.

So keeping that in mind ... you can create one application, call it www, and inside its views.py, define these very creatively named methods:

def blog(request):
   pass

def tasks(request):
   pass

Now in your urls.py, you can have:

url(r'^blog/$','www.views.blog',name='blog-index'),
url(r'^blog/posts/$','www.views.blog',name='blog-posts',kwargs={'view_posts': True}),
url(r'^tasks/$','www.views.tasks',name='task-index'),
url(r'^tasks/attachments/$','www.views.tasks',name='task-attachments'),

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon