创新不关乎肯定一切，而是关乎对所有重要的特性说不 ---Steve Jobs
1. 直接和应用的所有者沟通即使他们没有技术背景。 2. 确保你完整的倾听他们的需求并提醒他们。 3. 不要使用“模型”这一类的技术术语。保证用语简单，使用终端用户能理解的术语比如“用户账户”。 4.合理的期望值。如果某件事情在技术行不可行，或者很难实现，要保证你以正确的方法告知他们。 5.描述的尽可能具象。在大自然中人类是依靠视觉。网站更是如此。使用粗线条和图形表。不需要多么的完美。 6.分解处理流程，比如用户注册。任何多步骤的功能都需要以用箭头连接的方框画出。 7.最后，解决用户叙述表格中的特性， 8.扮演一个活动的角色 9.在融合新特性上要非常，非常地保守。 10.开会，和大家分享你的笔记以避免误解。
在本书，我们已经着手建构我们自己的一个为超级英雄而构建的称做SuperBook的社会网络。A simple sketch based off our discussions with a bunch of randomly selected superheroes is shown as follows:
Actually, in my early days, I was a bit of a kleptomaniac. I am allergic to nuts, you know. Other bros have it easy. They can just live off any park. I had to improvise—cafes, movie halls, amusement parks, and so on. I read labels very carefully too.
Ok, Acorn. Why do you think you were chosen for the user testing?
Probably, because I was featured in a Y tar special on lesserknown superheroes. I guess people find it ausing that a squirrel can use a MacBook (Interviewer: this interview was conducted over chat). Plus, I have the attention span of a squirrel.
Based on what you saw, what is your opinion about SuperBook?
I think it is a fantastic idea. I mean, people see superheroes all the time. However, nobody cares about them. Most are lonely and antisocial. SuperBook could change that.
What do you think is different about Superbook?
It is built from the ground up for people like us. I mean, there is no "Work and Education" nonsense when you want to use your secret identity. Though I don't have one, I can understand why one would.
Could you tell us briefly some of the features you noticed?
Sure, I think this is a pretty decent social network, where you can:
• Sign up with any user name (no more, "enter your real name", silliness) • Fans can follow people without having to add them as "friends" • Make posts, comment on them, and re-share them • Send a private post to another user
Everything is easy. It doesn't take a superhuan effort to figure it out. Thanks for your time, Acorn.
在构建web应用的前几天，像Photoshop和Flash这样的工具做到像素级别的模型效果使用是非常广泛的。 They are hardly recommended or used anymore.
Creating an HTML mockup is a lot faster and easier than before. If your web designer is unavailable, developers can use a CSS framework such as Bootstrap or ZURB Foundation framework to create pretty decent mockups.
A good mockup can give 80 percent of customer experience with less than 20 percent of the overall development effort.
Whichever approach you take, it is best to stop and think—"Which are the different ways in which I can ipleent this? What are the tradeoffs? Which factors are ore iportant in our context? Finally, which approach is the best?
Experienced Django developers look at the overall project in different ways. ticking to the DRY principle or soeties because they get lay, they think Have I seen this functionality before? For instance, can this social login feature be implemented using a third-party package such as django-all-auth? If they have to write the app themselves, they start thinking of various design patterns in the hope of an elegant design. However, they first need to break down a project at the top level into apps。
One of Django's biggest strengths is the huge ecosystem of third-party apps. At the time of writing, djangopackages.com lists ore than , packages. You ight find that your copany or personal library has even ore. nce your project is broken into apps and you know which kind of apps you need, you will need to take a call for each app—whether to write or reuse an existing one.
It might sound easier to install and use a readily available app. However, it not as simple as it sounds. Let's take a look at some third-party authentication apps for our project, and list the reasons why we didn't use them for SuperBook at the time of writing:
• Over-engineered for our needs: We felt that python-social-auth with support for any social login was unnecessary • Too specific: Using django-facebook would mean tying our authentication to that provided by a specific website • Python dependencies: One of the requirements of django-allauth is python-openid, which is not actively maintained or unapproved • Non-Python dependencies: Some packages might have non-Python dependencies, such as Redis or Node.js, which have deployment overheads • Not reusable: Many of our own apps were not used because they were not very easy to reuse or were not written to be reusable
None of these packages are bad. They just don't meet our needs for now. They might be useful for a different project. In our case, the built-in Django auth app was good enough. On the other hand, you might prefer to use a third-party app for some of the following reasons:
• Too hard to get right Do your odel's instances need to for a tree? Use django-mptt for a databaseefficient ipleentation • Best or recommended app for the job: This changes over time but packages such as django-redis are the most recommended for their use case • Missing batteries: Many feel that packages such as django-model-utils and django-extensions should have been part of the framework • Minimal dependencies: This is always good in my book
So, should you reuse apps and save tie or write a new custo app? I would recommend that you try a third-party app in a sandbox. If you are an intermediate Django developer, then the next section will tell you how to try packages in a sandbox.
From time to time, you will come across several blog posts listing the "must-have Django packages". However, the best way to decide whether a package is appropriate for your project is Prototyping.
Even if you have created a Python virtual environment for development, trying all these packages and later discarding them can litter your environment. So, I usually end up creating a separate virtual environment named "sandbox" purely for trying such apps. Then, I build a small project to understand how easy it is to use.
Later, if I am happy with my test drive of the app, I create a branch in my project using a version control tool such as Git to integrate the app. Then, I continue with coding and running tests in the branch until the necessary features are added. Finally, this branch will be reviewed and merged back to the mainline (sometimes called master) branch.
Accounts（定制）： 这个app提偶那个额外的用户账户信息。 Posts（定制）： 这个app提供发表和回复功能 Pows（定制）： 这个app跟踪有多少“碰”（支持或者喜欢） Boostrap forms（crispy-forms）： 这个app处理表单布局和风格
While preparing a development environment, make sure that you have the following in place:
• A fresh Python virtual environment: Python 3 includes the venv module or you can install virtualenv. Both of them prevent polluting your global Python library. • Version control: Always use a version control tool such as Git or Mercurial. They are life savers. You can also ake changes uch ore confidently and fearlessly. • Choose a project template: Django's default project template is not the only option. Based on your needs try others such as twoscoops (https://github.com/twoscoops/django-twoscoops-project) or edge (https://github.com/arocks/edge). • Deployment pipeline: I usually worry about this a bit later, but having an easy deployment process helps to show early progress. I prefer Fabric or Ansible.
This book believes in a practical and pragmatic approach of demonstrating Django design patterns and the best practices through examples. For consistency, all our examples will be about building a social network project called SuperBook.
SuperBook focusses exclusively on the niche and often neglected market segment of people with exceptional super powers. You are one of the developers in a tea comprised of other developers, web designers, a marketing manager, and a project manager.
The project will be built in the latest version of Python (Version 3.4) and Django (Version 1.7) at the time of writing. Since the choice of Python 3 can be a contentious topic, it deserves a fuller explanation.
While the development of Python started in , its first release, Python ., was released on December 3, 2008. The main reasons for a backward incompatible version were—switching to Unicode for all strings, increased use of iterators, cleanup of deprecated features such as old-style classes, and some new syntactic additions such as the nonlocal statement.
The reaction to Python 3 in the Django community was rather mixed. Even though the language changes between Version 2 and 3 were small (and over time, reduced), porting the entire Django codebase was a significant igration effort.
n February , Django . becae the first version to support Python . Developers have clarified that, in future, Django will be written for Python with an ai to be backward compatible to Python 2.
For this book, Python 3 was ideal for the following reasons:
就本书来说，Python 3 由于以下几个原因看来它是最理想的：
• Better syntax This fixes a lot of ugly syntaxes, such as izip, xrange, and unicode, with the cleaner and more straightforward zip, range, and
• Sufficient third-party support: Of the top 200 third-party libraries, more than 80 percent have Python 3 support.
• No legacy code: We are creating a new project, rather than dealing with legacy code that needs to support an older version.
• Default in modern platforms: This is already the default Python interpreter in Arch Linux. Ubuntu and Fedora plan to complete the switch in a future release.
• It is easy: From a Django development point of view, there are very few changes, and they can all be learnt in a few minutes.
The last point is important. Even if you are using Python 2, this book will serve you fine. Read appendix to understand the changes. You will need to make only minimal adjustments to backport the example code.
Ideally, every Django project should be in its own separate virtual environment. This makes it easy to install, update, and delete packages without affecting other applications. In Python 3.4, using the built-in venv module is recommended since it also installs pip by default:
$ python3 -m venv sbenv $ source sbenv/bin/activate $ export PATH="`pwd`/sbenv/local/bin:$PATH"
These commands should work in most Unix-based operating systems. For installation instructions on other operating systems or detailed steps please refer to the README file at the ithub repository https://github.com/DjangoPatternsBook/ superbook. In the first line, we are invoking the Python . executable as python3; do confir if this is correct for your operating syste and distribution.
The last export command might not be required in some cases. If running pip freeze lists your system packages rather than being empty, then you will need to enter this line.
$ git clone https://github.com/DjangoPatternsBook/superbook.git $ cd superbook $ pip install -r requirements.txt
$ cd final $ python manage.py migrate $ python manage.py createsuperuser $ python manage.py runserver
In Django 1.7, the migrate command has superseded the syncdb command. We also need to explicitly invoke the createsuperuser command to create a super user so that we can access the admin.
You can navigate to http://127.0.0.1:8000 or the URL indicated in your terminal and feel free to play around with the site.
Beginners often underestimate the importance of a good requirements-gathering process. At the same time, it is important not to get bogged down with the details, because programming is inherently an exploratory process. The most successful projects spend the right amount of time preparing and planning before development so that it yields the maximum benefits.
We discussed many aspects of designing an application, such as creating interactive mockups or dividing it into reusable components called apps. We also discussed the steps to set up SuperBook, our example project.
In the next chapter, we will take a look at each component of Django in detail and learn the design patterns and best practices around them.