Beginning Django E Commerce
398 pág.

Beginning Django E Commerce


DisciplinaProgramação I24.414 materiais278.198 seguidores
Pré-visualização50 páginas
Maybe it will make them laugh, and maybe that\u2019s a bad thing. Remember that the design in 
this book is just a placeholder. When you have the resources and the wherewithal, you can hire a 
designer to overhaul your catalog.html file and make it look the way you want. 
A quick note on CSS: for very large sites, you might want to break out your CSS into multiple files 
and import them into the one that you include in the link element in your site\u2019s head. You could 
conceivably put this into your css.css file (must be the first thing in the file): 
@import 'layout.css'; 
@import 'styles.css'; 
@import 'links.css'; 
The content of these three other files will be downloaded with your one style sheet. This can be 
extremely useful for partitioning areas of your CSS, especially for large projects, but be aware that each 
of your @import directives makes the loading of your site require an additional HTTP request. For 
performance optimization, you don\u2019t want to go \u201cimport crazy\u201d and have 20 imports for relatively small 
www.it-ebooks.info
CHAPTER 2 \u25a0 CREATING A DJANGO SITE 
 
34 
 
files. If your project will be fairly tiny, like mine, it\u2019s probably better for you to do as I will, and just use a 
single CSS file. 
Location, Location, Location 
We are repeating ourselves in the settings.py file with regard to one particular configuration: the path 
to our project on the system on which it\u2019s running. For example, there are two places in settings.py 
and one place in urls.py where the following path is located: 
/home/smoochy/eclipse/workspace/ecomstore\u2026 (etc) 
Clearly, this is suboptimal and we want to not only have this information declared in one place, but 
it should also be determined for us automatically, at runtime, based on the project environment. 
As it turns out, Python comes with a very simple function that returns the path to the current 
directory as a string. The following code will do this and return the path as a Unicode string: 
import os 
os.path.abspath(os.path.dirname(__file__).decode('utf-8')) 
Simple as that. One problem with this approach is that it will not work on Windows. If you run this 
function on a Windows box, the directory delimiters will be two backslashes: one that\u2019s really supposed 
to be a backslash and one that\u2019s escaping the backslash character. So, we can perform a simple find and 
replace on our string in order to get this working correctly on Windows boxes by replacing double 
backslashes with single forward slashes. 
Let\u2019s set a constant in our settings.py file that encapsulates this information. In your 
settings.py file, enter the following two lines of code somewhere near the top of the file: 
import os 
# hack to accommodate Windows 
CURRENT_PATH = \ufffd 
 os.path.abspath(os.path.dirname(__file__).decode('utf-8')).replace('\\', '/') 
Now, wherever you reference the path to your project in your configuration files, just replace the 
text with the CURRENT_PATH variable. Here is my TEMPLATE_DIRS tuple reconfigured: 
TEMPLATE_DIRS = ( 
 os.path.join(CURRENT_PATH, 'templates'), 
) 
Here we are using another method that ships with the os.path module. The join() function takes 
the CURRENT_PATH variable and appends the templates subdirectory to it. Now, if your project moves 
around between multiple machines during development (as mine tends to do), and regardless of where 
it ends up on the system when you deploy the site, you don\u2019t need to alter these directory paths after 
each move. 
www.it-ebooks.info
CHAPTER 2 \u25a0 CREATING A DJANGO SITE 
 
35 
 
A Site Navigation Include 
Things are set up for us to start developing the site pages, but I\u2019m going to show you one last template 
item briefly before we continue. The placeholder reading [navigation here] will be replaced by a set 
of anchor elements leading to the pages on our site. This is simple enough for HTML gurus that we can 
do this now. 
Create a directory within your templates folder called tags, and create a new file inside of your 
tags directory called navigation.html. Put the following HTML into that file: 
<ul> 
 <li><a href=&quot;/catalog/&quot;>Home</a></li> 
 <li><a href=&quot;/catalog/&quot;>About</a></li> 
 <li><a href=&quot;/catalog/&quot;>Privacy</a></li> 
 <li><a href=&quot;/catalog/&quot;>Products</a></li> 
 <li><a href=&quot;/catalog/&quot;>Contact</a></li> 
</ul> 
<div class=&quot;cb&quot;></div> 
Next, open up your project\u2019s CSS file and add in the following styles for our navigation list of 
hyperlinks: 
#navigation{ 
 width:900px; 
 height:auto; 
 background-color:#98AC5E; 
 text-align:middle; 
} 
#navigation ul{ 
 list-style:none; 
} 
#navigation ul li{ 
 float:left; 
 margin-right:15px; 
} 
#navigation ul li a{ 
 color:White; 
 font-weight:bold; 
 text-decoration:underline; 
} 
#navigation ul li a:hover{ 
 color:#616161; 
 background-color:White; 
 text-decoration:none; 
} 
You should now have this file set up in your project at: /templates/tags/navigation.html. We\u2019re 
going to use a Django templating option that allows us to separate out the template code, by using an 
www.it-ebooks.info
CHAPTER 2 \u25a0 CREATING A DJANGO SITE 
 
36 
 
include directive. Open up your catalog.html file and find the placeholder for the navigation. Replace 
it with the following line: 
{% include 'navigation.html' %} 
The include directive looks in your template directories for the file you specified, and if it finds it, 
inserts the HTML it finds in the template file into that spot in your template. If you come from a PHP 
background, this should look very familiar to you. This template has nothing to do with our 
inheritance model we structured earlier. Instead, it\u2019s merely an easy means of breaking out chunks of 
HTML in your site (in this case, the navigation) into a separate file that you can inject in your template 
where you want. This helps keep your templates more manageable, as it allows you take large blocks 
of HTML content and keep them out of your base templates, which should only contain markup 
related to the overall site structure. 
So, with this code in place, load up the catalog page. If you get an error, and a message on the error 
page that says something like TemplateSyntaxError and a message about an exception that was caught 
while rendering the exception, then bravo. 
You haven\u2019t done anything wrong. Remember earlier in the chapter when I promised you a Django 
template exception to test our error handling code? Our include tag is incorrect because we didn\u2019t 
specify the correct directory. Remember, we put the navigation.html file into 
/templates/tags/navigation.html, and not just in our /templates directory. Our Django project is 
looking in the templates directory for the file, and not inside any subdirectories. 
So, to fix your include tag, alter it so that it looks like this: 
{% include &quot;tags/navigation.html&quot; %} 
Try reloading your page again. You should be greeted with the same page as before, except now you 
should have five navigation links at the top, styled according to your CSS (and that, when clicked, don\u2019t 
take you anywhere). 
So let\u2019s check for that exception you just caused. Drop down into your database, either using the 
manage.py dbshell or otherwise, and run the following: 
SELECT * FROM djangodblog_error; 
You should get back at least one row, and the last one should contain the same details about the 
TemplateSyntaxError that were on the page when you encountered the exception. So why did we take 
the trouble to log the details about Django exceptions when we can plainly see the details about 
exceptions on the pages that throw them? 
As mentioned in the last section, in a production setting, there will be one crucial difference: your 
site will not be running in debug mode. In the settings.py file, near