Beginning Django E Commerce
398 pág.

Beginning Django E Commerce

DisciplinaProgramação I24.771 materiais281.850 seguidores
Pré-visualização50 páginas
into this store 
during a slow sales month and blaming the cash register. It\u2019s akin to saying, \u201cOh, this thing customers 
are using to swipe their credit cards should be on the right side of the counter, not the left! That\u2019s why 
people aren\u2019t buying anything!\u201d 
The reason your store, either online or off, is suffering is because of market conditions. There might 
be another e-commerce site that launched and is selling a lot of the same products you are for cheaper. 
Maybe those keywords that you bought from AdWords as a part of your search engine marketing 
campaign have shifted and are not yielding you the same value for your money. Maybe the economy just 
took a nose dive and people are wary about buying anything that doesn\u2019t fit into the lifestyle of a bare 
I\u2019m not saying that you should never add features to your site; I\u2019m merely saying that you should 
carefully consider the conclusions you draw when trying to figure out what\u2019s wrong with your site. You 
should add new features and functionality when you think they will add value to your users and their 
experience using your site, not because you\u2019re looking for a quick way to tow your site out of a ditch. In 
most cases, it\u2019s probably not the answer. 
In consulting, it\u2019s possible you will not always be able to get around this, even if the request is pretty 
silly. Perhaps, because they\u2019re your customer and they\u2019re paying you, you\u2019ll have to spend copious 
amounts of developer time implementing Feature X for them, against all better judgment in your mind. 
This is one of the annoyances of being a developer. The only thing you can do is offer your opinion and 
hope that, from one professional to another, they at least consider your point of view. Just try to 
remember that while silly requests like this might be the bane of your existence as a consultant, they are 
also your livelihood. After all, they are paying you. 

Before you dive into the rest of the book, take a moment to reflect on what you hope to learn about 
Django by reading this book. Skim the table of contents, and write down any questions that come up 
about material that you hope to take away from this chapter. Keep this list handy (perhaps next to the 
\u201cbrain dump\u201d plan we created at the beginning of the chapter) while you\u2019re reading the rest of the book 
and jot down the answers as you find them. For the questions that I don\u2019t cover, you can always refer to 
the online documentation later to fill in the gaps. 
The point of this assignment is to encourage you to learn with your goals in mind. There\u2019s a reason 
you\u2019re reading this, probably because there are things you\u2019re hoping to learn. Just make sure you know 
what they are before you start, and you\u2019ll be much more likely to retain the information. 
So, let\u2019s get started building our application. In the next chapter, we\u2019re going to create our Django 
site and set up our templates so that we have a basic skeleton for our e-commerce application.
C H A P T E R 2 
\u25a0 \u25a0 \u25a0 
Creating a Django Site 
Now that we have some idea about how our site is supposed to look and function fresh in our minds 
from the last chapter, we\u2019re going to get started by creating the basic skeleton for the site. 
First, we\u2019re going to create the Django site and have a look at the files that Django creates for you by 
default, and how you\u2019re supposed to use each one to set up the site. As every dynamic web site requires a 
back-end datastore, we\u2019re going to create and configure the MySQL database for our project. Then, we\u2019ll 
learn how Django maps requests to your site to view functions, and how these serve templates back to 
the users of your site. 
We\u2019re just going to create something very simple, but don\u2019t let this interfere with your own design 
needs. You\u2019re welcome to detract from my setup and use your own custom layout inside of our template 
files. You should be able to follow along with the technical concepts in the rest of the book using your 
own HTML and CSS. 
A Django-istic Welcome 
So, now that we have our development environment up and running, let\u2019s create our first Django site! 
Actually, when compared to getting Django installed and running properly, creating the site and getting 
to our first default \u201cWelcome!\u201d page is really a breeze. This is probably a good thing: after spending the 
first chapter plowing through our schemes and configuring all the software we\u2019re going to be using, I\u2019m 
sure you\u2019re ready for some instant gratification. 
Creating the Project 
The first thing to do, whether or not you\u2019ve opted to use the Eclipse IDE, is to fire up a shell on your 
system and navigate to the directory where you plan to store your project. On my system, this happens 
to be in /home/smoochy/eclipse/workspace. Once you\u2019re there, run the following command: 
$ startproject ecomstore 
Once you\u2019ve run this, go ahead and run ls to see that a directory called ecomstore was, in fact, 
created for you. If it\u2019s there, change into it and boot up your new site by doing the following: 
$ cd ecomstore 
$ python runserver 
You should see a few quick lines about validating Django\u2019s models, which I\u2019ll discuss in later 
chapters. More importantly, you should see a URL where you can view your new site in the browser.
Leave the shell open, fire up your favorite browser, and navigate to the URL the shell told you about. It 
should be http://localhost:8000/, or as The page you get should look 
something like Figure 2-1. 
Figure 2-1. Hello Django! 
Phew! That was easy! Actually, at the risk of being sued by the Staples corporation, I\u2019ll gladly amend 
the latter statement and say: That was Django! 
Notice that I named my project \u201cecomstore\u201d even though we had planned to call our store \u201cModern 
Musician.\u201d Why didn\u2019t I use the store name as the name of our project? The reason is that our project\u2019s 
code will use the project name in our Python code in a few places, and in the interest of keeping things 
reusable, I\u2019ve opted to use a more generic name. That way, in a year from now, when you clone your 
project in an effort to set up \u201cAmazing Athlete\u201d (or whatever), you\u2019ll easily be able to migrate over what 
you\u2019ve created. 
Let\u2019s look briefly at the files that are generated with a new Django site. 
What Django Creates 
Before we start exploring, if you want to open your new project using something other than the 
command line, open up Eclipse. Go to File New and select \u201cPyDev Project.\u201d Type ecomstore in as the 
name of your project, and set the directory as the path to the directory your project is in. (If you\u2019ve used 
the default, as I\u2019ve done, it should already be typed in for you.) Make sure you select the correct version 
of Python to correspond with what\u2019s on your system, then click \u201cFinish.\u201d After some quick whirring, the 
ecomstore folder should appear in Eclipse\u2019s file navigator. 
If you\u2019ve been following along, there should be at least four items in your ecomstore folder:,,, and Let\u2019s look at each one of these in turn and talk 
briefly about what it does: This file should be empty, and exists solely to tell your system 
that your ecomstore directory should be treated like a Python module.1 A local Python file that enables you to run various commands to 
administer your Django site. You\u2019ve already used it to boot up the local 
development server. 
1 Each directory on your system that you want to be recognized by Python should contain this file. One important 
point about these