Beginning Django E Commerce
398 pág.

Beginning Django E Commerce

DisciplinaProgramação I24.501 materiais279.087 seguidores
Pré-visualização50 páginas
files: they can contain Python code, and can be imported by importing the name of the 
directory. In this case, since our project in on our system\u2019s PYTHONPATH, then import ecomstore in a Python shell 
will import the code in the file.
19 The file responsible for mapping incoming requests to their 
respective responses. It\u2019s like the phone operator in the days before cell phones. 
When you put
bobblehead/ into your browser and you get back that product page, you can 
thank the file. A configuration file where you\u2019ll tell your site what database to 
use. This is the file where you would put site-wide configuration data, such as 
cookie expiration lengths, or credentials for a credit card processor. Using this 
file for such values instead of in your code makes the application easier to 
maintain, and makes these values easier to change, should you ever need to do 
so. This file can actually contain Python statements where necessary. 
We\u2019ll look at each of these in more detail as the need to do so arises. For now, though, let\u2019s take a 
moment to set up our MySQL database. 
Creating the MySQL Database 
In order to set up the database for our Django project, we need to do the following three things: 
1. Create a new database. 
2. Create a new MySQL user with a password. 
3. Grant the new MySQL user permissions to manipulate the database. 
We can\u2019t do these from within the Django project. For these, we need to open up MySQL directly 
and get our hands dirty. Don\u2019t worry... this isn\u2019t quite as difficult as it might sound. Open up your local 
MySQL server with root user access however you feel most comfortable. Since I\u2019m using a Unix variant, 
I\u2019m going to do this from the command line, but feel free to use something more GUI-friendly. Execute 
the following commands: 
mysql> CREATE DATABASE ecomstore CHARACTER SET utf8; 
Query OK, 1 row affected (0.00 sec) 
mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'password'; 
Query OK, 0 rows affected (0.00 sec) 
mysql> GRANT ALL ON ecomstore.* TO 'username'@'localhost'; 
Query OK, 0 rows affected (0.00 sec) 
Feel free to change the name of the database, the username, or the password (especially the 
password!) to suit your own situation. 

Before pressing on, one important note about MySQL tables. Internally, MySQL offers two different types of 
storage engines: MyISAM and InnoDB. By default, the tables you create use the MyISAM storage engine, 
which is missing the ability to handle transactions. Transactions basically allow you to execute multiple 
operations on data in an all-or-nothing fashion; that is, if anything goes wrong during one of the 
operations, then none of the changes are committed to the database. 
The InnoDB storage engine was designed for transaction processing, and can be particularly useful for 
certain situations in e-commerce web applications. We\u2019re not going to use transactions in this book, so 
we\u2019re going to keep to the default MyISAM storage engine for our database tables. However, you should 
consult the MySQL documentation2 for more about the differences between the two types to see which one 
better suits your needs. Also, you can read more about managing transactions in Django documentation.3 
Back to the database user we just created: why not just run the database as it stands right now? 
We\u2019re doing this for security reasons. When you start up MySQL for the first time, you\u2019re probably 
logging in as \u201croot\u201dwith the password you specified when you installed MySQL. This root user has 
privileges to execute any command on any database that it chooses. If you use the root username and 
password in your Django project, and someone compromises your Django files, then there\u2019s no limit to 
the damage they can do to all of the databases in your MySQL database server. Taking this extra step 
limits the damage they can do to only the ecomstore database. Consider it a containment strategy. 
More than likely, no outsider will ever hack into your application. A much more distinct possibility 
is as your site and organization grow, you may hire at least one more developer to help you maintain 
your application. No matter who you choose to hire, there\u2019s always a chance your professional 
relationship will end in a not-so-good manner and you want to make sure there\u2019s a limit to what they are 
able to do. Even if this particular case doesn\u2019t apply to you, it\u2019s important to be aware of the potential 
consequences of what you choose to do now. 
mysql utf-8 collation \u2013 what gives? 
You\u2019ll notice that we overrode the default character set when we created our database by explicitly setting 
it to UTF-8. This may or may not be what you want, and you\u2019re welcome to leave this setting off by 
eliminating the \u201cCHARACTER SET utf8\u201d portion of the CREATE DATABASE command. 
The fact that you\u2019re setting your database to store Unicode4 text is important because Unicode allows you 
to store accented characters, which customer names and addresses might contain. For this reason, you\u2019ll 
want your database to support the storage of Unicode text. Django also has a very adept 
internationalization framework, so you can display your web site in more than just one language. I\u2019m not 
going to cover the Django internationalization framework in this book, but if you ever want to start selling 
your products in, say, Russian, using the Cyrillic alphabet, you\u2019ll need your database to support Unicode 
text. You might want to look into the pros and cons of UTF-8 in your database, as well as how the collation 
affects your own business requirements. You can read more about the collation settings of your database 
in the MySQL documentation5. 
The easiest way to configure this is to add this setting when you create the database. This will ensure that 
all tables created, by default, use UTF-8 to store their data and return query results with the UTF-8 
character set. 
Now that we have our database set up and our ecomstore user configured, open up your file in your project. Start scrolling down... close to the top, you should see a list of database 
settings, starting with DATABASE_, all in caps. The settings should all be empty strings. Based on the 
database you just created, change the values to read as follows: 
DATABASE_ENGINE = 'mysql' # 'postgresql_psycopg2',... 
DATABASE_NAME = 'ecomstore' # Or path to database file... 
DATABASE_USER = 'username' # Not used with sqlite3. 
DATABASE_PASSWORD = 'password' # Not used with sqlite3. 
DATABASE_HOST = '' # Set to empty string for localhost.... 
DATABASE_PORT = '' # Set to empty string for default... 
Provided that you\u2019ve got your MySQL server installed locally, you can leave the host and port blank 
(it will assume localhost and the MySQL default port of 3306). 
The file includes a dbshell utility that will enter a MySQL command shell within your 
project\u2019s database, and it provides a means for you to test your database connection. Back in your 
system\u2019s shell, in your project\u2019s root, type the following: 
$ python dbshell 
This command should drop you into the MySQL command shell. You\u2019re now connected as the user 
you just created, and, as you had intended, you\u2019re