Reputation: 1454
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
...
Upvotes: 25
Views: 47491
Reputation: 2406
Each app has these files in their folders:
So, you have:
Project
-- manage.py
-- Project
-- -- views.py
-- -- models.py
-- -- ...
-- -- APP1
-- -- -- views.py
-- -- -- models.py
-- -- -- ...
-- -- APP2
-- -- -- views.py
-- -- -- models.py
-- -- -- ...
-- -- APPX
-- -- -- views.py
-- -- -- models.py
-- -- -- ...
The utility is the distinction between functionalities (call notification.models.Notification to access a Notification model inside a notification app).
A model is the architecture of an object. Django automatically creates database tables for each of your models after you execute the migration command. To access individual fields of these tables, use this syntax: Model.field (e.g., User.username, Task.name).
Use Django Admin interface to manually create and edit instances of your models. This interface is customizable (look here).
Additionally, you can create an identification system, custom forms, and use your own admin system.
WITH YOUR EXAMPLE:
<my project>
-- manage.py
-- <project name>
-- -- urls.py
-- -- models.py <--- User model exists in Django. You can customize it in this file if needed.
-- -- views.py
-- -- blog <--- It's an app.
-- -- -- -- views.py
-- -- -- -- urls.py
-- -- -- -- models.py <--- Post and Comment are models of the "blog" app. You define them here.
-- -- tasks <--- Another app.
-- -- -- attachments
-- -- -- -- views.py
-- -- -- -- urls.py
-- -- -- -- models.py <--- Models of the "tasks" app, i.e., Task model
Upvotes: 27
Reputation: 174708
Things you should understand about django:
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.
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'),
Upvotes: 18
Reputation: 599956
You are confused about what an app is. An app is just a collection of related functionality, usually (but not necessarily) including models, views and templates.
"Users" is not an app - it's a model, but you would usually use the built-in django.contrib.auth app to provide user functionality. Any app can use any other app's code, including models. And you can have as many models as you like in a single app.
Upvotes: 4