Beginning Django E Commerce
398 pág.

Beginning Django E Commerce


DisciplinaProgramação I24.476 materiais278.734 seguidores
Pré-visualização50 páginas
to strip out HTML tags, or HTML encode the content of the variable, from the output in 
your templates. 
In our simple example, we\u2019re basing the display of the word \u201ccategories\u201d on how many categories 
the product is in. If the category count is only one, the word \u201ccategory\u201d will be displayed. If the count is 
www.it-ebooks.info
CHAPTER 3 \u25a0 MODELS FOR SALE 
 
72 
 
more than one, however, than the word \u201ccategories\u201d will be used instead, through the use of the 
pluralize filter: In categor{{ categories.count|pluralize:"y,ies" }}. 
Next, notice that as we loop through our categories and output the links, there is a nested if loop 
that looks like this: 
{% if not forloop.last %}, {% endif %} 
The syntax is simple enough\u2026 this almost looks like pseudo-code. This delimits your category link 
list with commas, but the comma will not be appended to the last category because it is the last item in 
the loop. 
Next, we\u2019re using the custom sale_price() method that we specified in our product model. If you 
refer back to the code, this method will only return a value if an old price is set that is higher than the 
price field. So, our {% if %} block will only evaluate to true if the product is on sale. In this case, a 
\u201cbefore\u201d and \u201cafter\u201d price will be displayed, with a strike through the \u201cbefore\u201d price. If the product is not 
on sale, the price field is simply displayed. 
There are a few simple things we need to add before we get a sneak preview of our site. First, there 
are some new CSS classes, so open up your css.css file and add the following styles: 
a{ 
 color:#98AC5E; 
 font-weight:bold; 
 text-decoration:none; 
} 
a:hover{ 
 text-decoration:underline; 
} 
 
/* category page styles */ 
div.product_thumbnail{ 
 text-align:center; 
 float:left; 
 width:150px; 
 height:200px; 
} 
 
/* product page styles */ 
div.product_image{ 
 float:left; 
 padding:0 10px 10px 0; 
} 
Product images need a place to live on our site, and we\u2019ve already set aside a small corner inside our 
static directory. Create an images folder inside your static folder. Inside this, create a products 
folder. Inside here (provided you\u2019re not lost yet), create two directories for your products called main and 
thumbnails. If you have some product images already created for any of the products you\u2019ve added 
through the admin interface, go ahead and put them in here now. Be sure that they\u2019re sized correctly, 
since our templates don\u2019t resize our images in our template markup. 
www.it-ebooks.info
CHAPTER 3 \u25a0 MODELS FOR SALE 
 
73 
 
A Category Link List 
Now, we\u2019re going to use the list of active categories that all of our templates have as a variable, but 
currently are not configured to use in any way. Go into your catalog.html template file and find the 
line of text in your site\u2019s sidebar acting as a placeholder for your category list. Get rid of the [ category 
listing here ] and replace it with the following line of code: 
{% include "tags/category_list.html" %} 
Create the category_list.html file inside the tags subdirectory, and add this simple loop code to 
it: 
<h3>Categories</h3> 
{% for c in active_categories %} 
 <a href=&quot;{{ c.get_absolute_url }}&quot;>{{ c.name }}</a><br /> 
{% endfor %} 
This will list the active categories in your product catalog in the side navigation bar of your site. 
Our Code in Review 
At this point, take a moment to review the directory structure and files in your project. If you\u2019ve been 
following along, your project should be structure like this: 
ecomstore/ 
 catalog/ 
 __init__.py 
 admin.py 
 forms.py 
 models.py 
 tests.py 
 urls.py 
 views.py 
 static/ 
 images/ 
 products/ 
 main/ 
 image1.jpg 
 thumbnails/ 
 image1.jpg 
 css.css 
 templates/ 
 catalog/ 
 category.html 
 index.html 
 product.html 
 tags/ 
 category_list.html 
 navigation.html 
www.it-ebooks.info
CHAPTER 3 \u25a0 MODELS FOR SALE 
 
74 
 
 base.html 
 catalog.html 
 index.html 
 utils/ 
 __init__.py 
 models.py 
 views.py 
 tests.py 
 context_processors.py 
 __init__.py 
 manage.py 
 settings.py 
 views.py 
 urls.py 
Now that this is all set up properly, go ahead and fire up your development server from your shell 
using manage.py runserver, and navigate to the homepage. You should see your site load up with the 
\u201cWelcome!\u201d header that you created in your catalog/index.html template. 
\u25a0 Note Your site directory structure might also contain files with the extension .pyc. When you run your code, the 
Python interpreter will attempt to create compiled versions of your Python files. Compilation isn\u2019t necessary, as 
Python is an interpreted language; it\u2019s just a means of optimizing the speed of your code. 
You can see that our side navigation now has a list of categories displayed. Click on one that you 
know has products in it, and you should be taken to that category\u2019s page, complete with a listing of 
products. Click on one of the products, and see if that page loads correctly. My product page looks like 
Figure 3-5. Click around on the site for a while, make sure everything is working the way you expect it to. 
Make note of the URLs, and make sure you understand how they are structured. 
www.it-ebooks.info
CHAPTER 3 \u25a0 MODELS FOR SALE 
 
75 
 
 
Figure 3-5. My product page. 
You\u2019ll notice that the page contains a placeholder for the \u201cAdd to Cart\u201d button, which doesn\u2019t do 
anything yet. We\u2019ll be adding this functionality in the next chapter. 
So How Does It All Work? 
Let\u2019s walk through a request step by step, and review the way our code is handling all of this. A request to 
the site is first routed through the urls.py file in our root directory, and the requests to the homepage, 
category, and product views are now being routed through the one line with our include() function in 
it: 
(r'^', include('catalog.urls')), 
This in turn maps to the urls.py file we added to our catalog app. The urlpatterns variable in this 
file looks like this, in its entirety: 
urlpatterns = patterns('ecomstore.catalog.views', 
 (r'^$', 'index', { 'template_name':'catalog/index.html'}, 'catalog_home'), 
 (r'^category/(?P<category_slug>[-\w]+)/$', \ufffd 
 'show_category', { 
'template_name':'catalog/category.html'},'catalog_category'), 
 (r'^product/(?P<product_slug>[-\w]+)/$', \ufffd 
 'show_product', { 
'template_name':'catalog/product.html'},'catalog_product'), 
) 
Any incoming request to your site is going to go through the one in the root of your project first, and, 
when it gets to the line with the include() function, every last request will match it. Regular expressions 
use the ^ and $ characters for two very particular tasks in matching string patterns: the beginning and 
the end of the string pattern, respectively. A regular expression on your site that looks like 
www.it-ebooks.info
CHAPTER 3 \u25a0 MODELS FOR SALE 
 
76 
 
(r'^contact/$', 'contact_view_function') will match that inner string tacked on to the end of 
your site\u2019s domain. So this request would match that particular regular expression exactly: 
http://www.yoursite.com/contact/. 
This first pattern just uses the ^ without a corresponding $ character that will match everything, 
even an empty request to http://www.yoursite.com/. So, every request will continue on the urls.py 
file in your site\u2019s catalog app folder. When you get to