1、此文档是毕业设计外文翻译成品( 含英文原文+中文翻译),无需调整复杂的格式!下载之后直接可用,方便快捷!本文价格不贵,也就几十块钱!一辈子也就一次的事!外文标题:Introduction to the Django Framework外文作者:Daniel Rubio文献出处: Daniel Rubio . Introduction to the Django FrameworkM.Beginning Django.2017:1-29英文 8701 单词,40127 字符,中文 12987 汉字。Introduction to the Django FrameworkDaniel RubioT
2、he Django framework started in 2003, as a project done by Adrian Holovaty and Simon Willison at the Journal-World newspaper in Lawrence, Kansas, in the United States. In 2005, Holovaty and Willison released the first public version of the framework, naming it after the Belgian-French guitarist Djang
3、o Reinhardt.Fast forward to 2017 the Django framework now operates under the guidance of the Django Software Foundation (DSF), the framework core has over 1000 contributors with more than 15 release versions, and there are over 3000 packages specifically designed to work with the Django framework.1T
4、he Django framework has remained true to its origins as a Model-View-Controller (MVC) server-side framework designed to operate with relational databases. Nevertheless, Django has stayed up to date with most web development tendencies via third-party packages to operate alongside technologies like n
5、on-relational databases (NoSQL), real-time Internet communication, and modern JavaScript practices. All this to the point, the Django framework is now the web development framework of choice for a wide array of organizations, including the photo sharing sites Instagram2 and Pinterest3; the Public Br
6、oadcasting System(PBS)4; in the United States, National Geographic5; and with the help of this book, your organization!In this chapter youll learn about the Django framework design principles, which are key to understanding the day-to-day aspects of working with the Django framework. Next, youll lea
7、rn how to install Django in various ways: as a tar.gz file, with pip,using git, and with virtualenv.Once you install the Django framework, youll learn how to start a Django project and how to set it up with a relational database. Next, youll learn about the core building blocks in the Django framewo
8、rk urls, templates, and apps and how they work with one another to set up content. Finally, youll learn how to set up the Django admin site, which is a web-based interface designed to access the relational database connected to a Django project.Django Framework Design PrinciplesIf you work long enou
9、gh in web development, youll eventually come to the conclusion that you can produce the same results with just about any web framework and programming language. But while you can, in fact, produce identical results, what will vary drastically is the time you spend creating a solution: the time creat
10、ing a prototype, the time adding new features, the time doing testing, the time doing debugging, and the time deploying to scale, among other things.In this sense, the Django framework uses a set of design principles that produces one of the most productive web development processes compared to many
11、 other web frameworks. Note, Im not saying Django is a silver bullet (e.g., the best at prototyping, the most scalable); Im saying that at the end of the day, the Django framework incorporates a set of design principles and trade-offs that make it one of the most productive frameworks for building t
12、he features needed by most medium to large web applications.Now, while you might think Im biased after all Im writing an entire book about the topic Ill lay out these design principles first, so you can gain a better understanding of what gives the Django framework this edge.Dont Repeat Yourself (DR
13、Y) PrincipleRepetition might be good to emphasize a point, but when it comes to web development, it just leads to additional and time-consuming work. In fact, the very nature of web development, which operates across multiple tiers interacting with one another (e.g., HTML templates, business logic m
14、ethods, and databases), lends itself to repetition.The Django framework really tries to force you not to repeat yourself, so lets see how Django enforces not repeating yourself and why this is a good thing. Lets say you want to build a coffeehouse application to publish information about stores and
15、also have a contact form for customers. The first thing youll need to do is determine what kind of information is required for stores and the contact form. Figure 1-1 illustrates amock-up of two Django models for each of these entities.Figure 1-1. Django models for store and contact entitiesNotice h
16、ow the Django models in Figure 1-1 each have different field names and a data type to restrict values. For example, the statement name = model s. Char Fi el d( max_l engt h=30) tells Django a store name should have a maximum of 30 characters, while the statement emai l = model s. Emai l Fi el d( ) t
17、ells Django the contact entity should contain a valid email value. If the coffeehouse is like most web applications, youll generally end up doing the following for the store and contact entities: Create relational database tables to save entity information. Create business logic to ensure the entiti
18、es comply with requirements. Create HTML forms to allow data to be submitted for the entities. Create an interface to allow administrative users to access entities in the database. Create REST services to expose entities for a mobile app version.The crux of doing this last task list is you have the
19、potential of repeating dozens of similar pieces of information (e.g., names, value limits) in database definition language (DDL), HTML forms, business validation logic, and URLs, among other things process thats not only time consuming, but also error prone.Wouldnt it be easier that based on a state
20、ment like model s. Char Fi el d( max_l engt h=30) you could generate an HTML form input, a DDL statement, and automatically validate information to only contain 30 characters? This is exactly what Djangos DRYdesign principle does.Figure 1-2 illustrates the same Django models from Figure 1-1 and the
21、various constructs you can generate from the same models without the need to repeat yourself.Figure 1-2. Django models create separate constructs based on DRY principleAs you can see in Figure 1-2, the entities that represent Django models are capable of generating HTML forms to present to the publi
22、c, an administrative interface to manage the entities, validation logic to enforce entity values, as well as the DDL to generate database tables representing the entities.While its a little premature to discuss the actual techniques to generate such constructs from Django models, needless to say its
23、 much simpler than keeping track of multiple references of the same thing (e.g., name, email) in HTML forms, DDL, validation logic, and other locations.In this sense, Django really helps you define things in a single place and not have to repeat them elsewhere. Note that its always possible to repea
24、t yourself to obtain custom behaviors, but by default, Django enforces DRYprinciples in nearly everything you do with it.Explicit Is Better Than ImplicitPython, the programming language used by Django, has a mantra-like statement called “The Zen of Python” defined as part of the languages Python Enh
25、ancementProposals (PEP), specifically PEP 20.6 One of the statements in PEP 20 states “Explicit is better than implicit” and with Django being based on Python, this principle is also taken to heart.Being explicit leads to web applications that are easily understood and maintained by a greater number
26、 of people. Adding new features or understanding the logic behind a web application can be hard enough for someone that didnt write it originally, but if you toss into the mix constructs that have implicit behaviors, users only face greater frustration trying to figure out whats being done implicitl
27、y. Explicit does require a little more work typing, but its well worth it when you compare it to the potential effort you can face trying to debug or solve a problem.Lets take a quick look at Djangos explicitness in a common web development construct used across different MVC frameworks: a view meth
28、od. Aview method acts as the C(ontroller) in an MVC framework, charged with handling incoming requests, applying business logic, and then routing requests with an appropriate response.To get a better feel for this explicitness, Ill present a Django view method and an equivalent Ruby on Rails view me
29、thod that performs the same logic of fetching a store by a given id and routing the response to a template. The following snippet is the Ruby on Rails version; note the lines with # that are comments and indicate whats happening.cl ass St or esCont r ol l er Appl i cat i onCont r ol l er def show# A
30、ut omat i c access t o par ams, a r uby hash wi t h r equest par amet er s and vi ew par amet er s st or e = St or e. f i nd( par ams : i d )# I nst ance var i abl es l i ke st or e ar e aut omat i cal l y passed on t o vi ew t empl at e# Aut omat i cal l y uses t empl at e vi ews/ st or es/ show. h
31、t ml . er b endendAlthough very succinct, notice all the implicit behavior surrounding the process to access data, pass data to a template, and assign a template. The following snippet is an equivalent Django view method.# Expl i ci t r equest var i abl e cont ai ns r equest par amet er s# Ot her vi
32、 ew par amet er s must be expl i ci t l y passed t o vi ews def det ai l ( r equest , st or e_i d) :st or e = St or e. obj ect s. get ( i d=st or e_i d)# I nst ance var i abl es must be expl i ci t l y passed on t o a vi ew t empl at e # Expl i ci t t empl at e must be assi gnedr et ur n r ender ( r equest , st or es/ det ai l . ht ml , st or e : st or e)Notice in this last snippet theres no guessing where input parameters come from, theyre explicitly declar
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2