开发者

What exactly are Django Apps

开发者 https://www.devze.com 2023-04-01 04:11 出处:网络
I want to switch from Rails to Django, to broaden my mind, and a question has bobbed up in my mind. My Rails app is quite a mess, since my hobby-based development approach is a patch-and-glue one. I

I want to switch from Rails to Django, to broaden my mind, and a question has bobbed up in my mind.

My Rails app is quite a mess, since my hobby-based development approach is a patch-and-glue one. I have seen very early that Django divied between a project and an app. According to their site, a project is made of many apps, and one app can be used for many projects.

This intrigued me, since that would made the lines between my site's areas clearer. I tried to find some more e开发者_开发技巧xamples and information on that, but I couldn't answer my question, which is:

How big/small is such an app? Are they able/supposed to interact closely?

It is, for example smart to have one app to deal with user's profiles, and another app to deal with blog-posts and comments, from those users? (In my site, a user can have several blogs, with different profiles). Or are they meant to be used otherwise?


A django App is a fancy name for a python package. Really, that's it. The only thing that would distinguish a django app from other python packages is that it makes sense for it to appear in the INSTALLED_APPS list in settings.py, because it contains things like templates, models, or other features that can be auto-discovered by other django features.

A good django app will do just one thing, do it well, and not be tightly coupled to any other app that might use it. A wide variety of apps are provided with django in the contrib namespace that follow this convention.

In your example, a good way to devise apps is to have one for user profiles (or use one of the many existing profile apps), one app for blog posts (or one of the many that already do this), one app for comments, separate from blog posts (again, you can use an existing app for this), and finally, a very tiny app that ties the three together, since they don't and shouldn't depend on each other directly.


The purpose of using app's is to make them reusable. Django likes DRY principle DRY stands for DO NOT repeat yourself

An app should be as small as it can, and loosely coupled. So, for an app should not need another app to work properly.

Django recommends writing an app for each table (well, not always, but as soon as you want to grow your app, you will definitely need to divide tables to pair apps. Or else you will have hard time for maintaining your code.)

You can, for example, create an app for users, an app for sales, an app for comments, an app for articles. It will be easier to maintain your code and if you have done it right, you can use the app in other project with a little (if any) modification in the app.

Project's are, compilation of app's. Users app, articles app, comments app can come together an make a project, or in other words, a website.

If you want to learn django, I suggest you to check out:

http://www.djangobook.com/

http://docs.djangoproject.com/


One word of advice, do not, in any case, copy/paste. Not only your code has great chance to fail, but you will not know what the code is doing. If you are going to use someone elses code in your project, at least type them, this will make you understand what the code is doing, or at least, it will give an idea.

Writing your own code is always better for maintance, but this does not mean that you should reinvent the world, you can use libraries, look at their documentation to use it properly.

Documentations, tutorials are you best friend.

Good luck.


A project is basically a place where your project lives...in your project you setup your url's, project settings, etc.

An app defines its own data models and views to be used within a project. You can move these between projects if you like.

I highly recommend running through the tutorials from the Django site as they will show you what a project and app are, how to manage both, how to use the admin panel, how to make the app usable in multiple projects, etc.


A portal = A django project

A ads system, gallery photos, catalog of products = Apps

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号