Beginning Django E Commerce
398 pág.

Beginning Django E Commerce


DisciplinaProgramação I24.517 materiais279.343 seguidores
Pré-visualização50 páginas
problems with 
existing third-party solutions when they arise. On top of this, you might also find yourself working to 
extend the functionality of an existing solution to suit your own needs, which is a good ability to have as 
well. 
Who This Book Is For 
This book is aimed at developers who are interested in learning more about the process of how to create 
a Django web site. Over the course of the book, we're going to create a single working e-commerce web 
site that we'll deploy into production at the very end. In each chapter, we'll tackle a particular feature or 
group of features that we want to add to the site, outline the requirements and discuss the related 
concepts, and then write code to implement each feature using Django. This will allow you to see how 
the different parts of a single Django project all fit together. In the end, you'll have a thorough grasp of 
www.it-ebooks.info
\u25a0 INTRODUCTION 
 
xx 
how a Django web application is created, secured, optimized for search engines, tested, and finally 
deployed. 
We're going to create a shopping cart site in this book, and while some of the sections cover 
problems that are specific to e-commerce web sites, most of the content has applications to other types 
of sites as well. For example, Chapter 8 covers implementation of internal site search so that customers 
can find things in our product catalog. Search functionality is a requirement of almost any data-driven 
web site. In Chapter 4, we create a shopping cart for our product catalog, allowing customers to 
aggregate products before they check out, and here you'll learn more about how you can use Django 
sessions in order to track information about your customers, whether or not they are logged in. The 
checkout functionality created in Chapter 5 covers the basics of Python network programming in order 
to integrate with third-party payment gateways, and the material covered is useful to anyone interested 
in integrating Django with web services. 
This book does assume familiarity with the Python programming language. If you're a complete 
beginner and have never worked with Python, don't worry... it's a very simple language and you'll be 
able to catch on very quickly. If you're new to programming and would like an introduction, I'd suggest 
you take a look at Beginning Python: Second Edition, by Magnus Lie Hetland (Apress, 2008). If you're 
already familiar with at least one other programming language and just need to get caught up on the 
syntax of Python, I can heartily recommend you read Dive Into Python, by Mark Pilgram (Apress, 2004). 
The Web Sites In This Book 
In this book, I'm going to build a fictional e-commerce site that sells musical instruments and sheet 
music. The name of the site is "Modern Musician." Developers in the Django community have a 
penchant for naming their apps and projects after old-time musicians, like John Coltrane, Louis 
\u201cSatchmo\u201d Armstrong, and Duke Ellington. This tradition was started by the creators of the Django web 
framework, who chose to name it after guitarist Django Reinhardt, who is regarded by many as one of 
the greatest jazz guitarists of all time. 
It didn\u2019t dawn on me until around the time I started writing Chapter 13 that the name "Modern 
Musician" might be construed as a tongue-in-cheek reference to this tradition in the Django 
community. In my defense, I originally created the Modern Musician e-commerce site in PHP, as a 
demo e-commerce site. Later, I implemented roughly the same Modern Musician site using Ruby on 
Rails, for the sole purpose of learning Rails. So when I got around to spawning this little project in 
Django, the last thing on my mind when naming the project was any attempt at ridicule. I did so out of 
tradition. 
In the first 15 chapters of this book, we\u2019re going to build a single e-commerce web site. For those 
interested, the site we\u2019re going to create is available for public viewing at http://www.django-
ecommerce.com/. While an administrative interface is part of the site that we\u2019re going to create in this book, 
the public site does not permit altering of data in the product catalog. 
In Chapter 16, when we look at putting Django projects up on the Google App Engine, we\u2019re going to 
create a minimal shopping cart site, which is also available for public viewing at http://django-
ecommerce.appspot.com/. 
Source Code and Errata 
We\u2019re going to write a lot of code in this book. It\u2019s not an overbearing amount, as Python is a very 
concise language and Django syntax tends to reduce the amount of repetitive code that you need to 
write. In spite of this, you still might find yourself wanting to have a copy of the code on hand so that you 
www.it-ebooks.info
\u25a0 INTRODUCTION 
 
xxi 
 
don\u2019t have to type in every last line. The source code for the site we\u2019re going to create in this book is 
available for download from the Apress web site.1 (There\u2019s also a bunch of awesome books on there.) 
If you happen to be reading a digital version of this book on your computer and have the option of 
selecting text, I\u2019d be careful about copying code from your screen into your editor. Some characters 
might not transfer from electronic versions of this book into IDEs very well, as they might confuse the 
Python interpreter. You\u2019re much safer just typing it in yourself. You\u2019ve been warned. 
Lastly, while everyone has worked really hard to ensure that this book is grammatically and 
technically correct, some grammatical and technical \u201cbugs\u201d may have slipped in under our reviewing 
eyes. (\u201cBug\u201d is a nice euphemism for \u201cmistake,\u201d isn\u2019t it?) If you find an error and would like to politely 
rub it in my face, please feel free to submit it to this book\u2019s errata page on the Apress web site.2 
If you\u2019d like to contact me with any questions or concerns you have about the content of this book, 
shoot me an e-mail at: jim@django-ecommerce.com. 
 
1 http://www.apress.com/book/sourcecode 
2 http://www.apress.com/book/view/1430225351 
www.it-ebooks.info
C H A P T E R 1 
 
\u25a0 \u25a0 \u25a0 
 
1 
 
 
Best Laid Plans 
Web development starts in the mind. Sure, it may be done in Python, Ruby, C#, Groovy, or any of the 
other myriad programming languages out there these days, but all of them are just tools. The code for 
web sites is created in the mind. Conception occurs in the space (or lack of empty space, hopefully) 
between your ears, and implementation occurs in your programming language of choice. 
One of the main goals of this book is to teach you how to architect a site using the Django web 
framework. But architecting a site, like any piece of software you might be developing, is about a 
sequence of decisions you need to make for yourself. Some of these decisions are small; others are quite 
large. A lot of them don\u2019t have an easy answer or one that is immediately obvious. 
While I can\u2019t answer your questions for you, and even though my decisions might end up very 
different from your own, I\u2019m going to talk you through the process. In this way, I hope to show how to 
translate your train of thought, and the decisions you make along the way, into workable software. And I 
hope that it makes you some money in the process. 
In this book, we\u2019re going to develop an e-commerce application. I chose this kind of web site for a 
few reasons. First, there is money in selling products online. It\u2019s still very possible for would-be 
entrepreneurs to conceive of business plans that are financially solvent, solely based on selling stuff to 
people on the web. For this reason, there is likely a demand for this type of application, and an interest 
in seeing how it\u2019s done. 
Second, I think e-commerce is interesting. An e-commerce project using any particular framework 
tends to be fairly complex, with lots of ins and