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
...
Each app will have these in their folder:
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
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'),
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With