0

Sorry for this dumb question, but i was confused:

I read This

Intermezzo: Projects vs. Apps

You may be wondering why, back in Django 1.4, the startproject command was added alongside the pre-existing startapp command. The answer lies in the difference between Django "projects" and Django "apps". Briefly, a project is an entire web site or application. An "app" is a small, (hopefully) self-contained Django application that can be used in any Django project. If you're building a blogging application called "Super Blogger", then "Super Blogger" is your Django project. If "Super Blogger" supports reader polls, "polls" would be an Django app used by "Super Blogger". The idea is that your polls app should reusable in any Django project requiring polls, not just within "Super Blogger". A project is a collection of apps, along with project specific logic. An app can be used in multiple projects.

While your natural inclination might be to include a lot of "Super Blogger" specific code and information within your "polls" app, avoiding this has a number of benefits. Based on the principle of loose coupling, writing your apps as standalone entities prevents design decisions and bugs in your project directly affecting your app. It also means that, if you wanted to, you could pass of the development of any of your apps to another developer without them needing to access or make changes to your main project.

Like many things in software development, it takes a bit of effort up-front but pays huge dividends later.

Then, if the website contains: Upload, Login, Registration, Add post

do i then split them to projects if i follow the app philosophy (instead of using on file for each Controller/View)? isent another complication for the code?

Since Django created the __init__ files to make it easier to import the class then what is the main idea to complicate it with other startapp? in tutorials i find in the web, all of them make the startapp once, even the most complete applications.

Community
  • 1
  • 1
Abdelouahab
  • 7,331
  • 11
  • 52
  • 82
  • I don't understand your question. You certainly would not have one file for each view, whether or not you split them up into separate apps. – Daniel Roseman Oct 24 '14 at 19:10
  • Please explain what models you need, and I will be happy to lay out an architecture along the lines of the cited source for you. It seem like good advice you are citing there. – Robert Jørgensgaard Engdahl Oct 24 '14 at 19:20
  • Might be answered here: Django projects vs apps [1]: http://stackoverflow.com/questions/4879036/django-projects-vs-apps?rq=1 – DreaminVM Oct 24 '14 at 19:41
  • the problem is about adopting every view with its own project, since this is what i understood? – Abdelouahab Oct 24 '14 at 20:03
  • But that isn't even vaguely what the quoted text says. A project is an entire website; an app is a single area of functionality. Neither of these are composed of single views. – Daniel Roseman Oct 24 '14 at 22:18
  • a poll is a part of the website, why not making it inside the `views.py` file and assign him its `controller` ? why making a whole other application ? – Abdelouahab Oct 24 '14 at 22:25

1 Answers1

1

A project is made up of apps. You can have an app to manage users (Login, registration, etc). Another one for app to manage blogs, etc.

All the functionalities should be self-contained, keeping them independant as much as possible. That way you can alter the functionality of your login without making your blog system crash. Every self contained functionality should be an app (users, polls, blogs, comments, etc).

It may be hard to keep them completely isolated, that's the reason why you use external apps that were designed with specific purposes and can be plugged into your project easily. Don't do by yourself what you could do with a good app unless it is absolutely necessary (licenses, extra features, etc).

You use one file for urls, another one for views and another one for models per each app. Your views.py for users can contain all the views that handle all you need, your models.py contains all the models your users require and your urls.py ties everything together. This is an example of how everything should work in an app. You don't need 1 file per view/controller, you need 3-5 files per app but those are required to keep a modular and structured design.

cdvv7788
  • 2,021
  • 1
  • 18
  • 26
  • and why then not using the `classes` within the same file? since it is a `class` then it can be imported next time in another project, no? – Abdelouahab Oct 24 '14 at 20:04
  • Everything you use are classes...views (those can actually be functions too but i prefer class based views), models, tests, etc. You just group simmilar classes withing a single file. In case you have not finished the [django tutorial](https://docs.djangoproject.com/en/dev/intro/tutorial01/)...do that...it will help you understand that kind of concepts. – cdvv7788 Oct 24 '14 at 20:07
  • yes, but the problem is that all django tutorials i seen make the application once, and put everything there! untill now, i dident see someone making `startapp` more than once! – Abdelouahab Oct 24 '14 at 20:09
  • If you want to make something that can be imported in another project you need to make sure it is completely isolated from code that you are not exporting. You can read [django documentation](https://docs.djangoproject.com/en/dev/intro/reusable-apps/) where the basics for this are explained. – cdvv7788 Oct 24 '14 at 20:10
  • but a normal website, like: registration/login/add-comment will not need so much overhead? – Abdelouahab Oct 24 '14 at 20:12