Beginning Django E Commerce
398 pág.

Beginning Django E Commerce

DisciplinaProgramação I24.501 materiais279.087 seguidores
Pré-visualização50 páginas
{% with active_categories as cats %} 
 {% for c in cats %} 
 {% ifequal c.get_absolute_url request_path %} 
 {{ }}<br /> 
 {% else %} 
 <a href=&quot;{{ c.get_absolute_url }}&quot; class=&quot;category&quot;>{{ }}</a><br /> 
 {% endifequal %} 
 {% endfor %} 
{% endwith %} 
Here, on top of the {% with %} tag again, we\u2019re also using a new template tag, the {% ifequal %} tag, 
which takes two parameters, compares them, and returns True if they have the same value. As you can 
see, the function takes the current request.path (in a variable called request_path) and compares it with 
the get_absolute_url() returned by our category. If the two are equal, that means the user is on that 
particular category page, and we deactivate the link to just be plain text. 
There are some CSS styles that go along with this. Add these to your CSS file: 
ul#categories li{ 
ul#categories li a{ 
div#footer a{ 
And finally, we just need to tweak the catalog.html file so that our site uses our new template tag 
instead of the include tag we created before. Find this line: 
{% include 'tags/category_list.html' %} 
and change it to this: 
{% category_list request.path %} 
You\u2019re all set. Your category link list at the side of the site now has its data call and functionality 
neatly wrapped up in one template file and one template function. At this point, you can go back to your file in your catalog app and remove the variable active_categories from your 
return dictionary, as you don\u2019t need this to be called here anymore.
Static Content with Flatpages 
In the next chapter, we\u2019re going to create the site checkout and order processing system, and before we 
do that, we need to set up a bunch of pages with static HTML content on them. Customers who shop 
online, particularly at small sites, will be interested in your organization\u2019s privacy policy. People want to 
be reassured that you\u2019re not going to hand out their e-mail and phone number to every spammer on the 
planet, who will then try to contact them at every possible opportunity to buy a renewal on the warranty 
on their vehicle which is about to expire, and what have you. 
There are lots of other pages you might want to create, such as a \u201cContact\u201d or \u201cAbout\u201d page, for 
which we set up placeholders in Chapter 2 in our navigation.html file. You might also need to create a 
\u201cReturn Policy\u201d page so that your customers can be reassured that they\u2019ll be taken care of in the event 
that their product ends up being broken on arrival, or any of the other things that can go wrong during 
The need for these static pages, that will only contain a header and some basic text as content, is a 
very wide one. Since it\u2019s such a common requirement for most sites, Django comes equipped with an 
app called flatpages that is designed to help you create simple static pages. 
Hooking up the flatpages app is just as simple as adding the app to your project. Open your file and add the following line to your INSTALLED_APPS tuple: 
Then, add this line to your MIDDLEWARE_CLASSES section: 
Finally, run the syncdb utility in order to set up your database tables for the models: 
$ python syncdb 
If that runs successfully, you can now add static pages to your admin interface. Go ahead and fire up 
http://localhost:8000/admin/ in a browser window, log in, and notice that there\u2019s now a section of your 
admin interface named \u201cFlat pages.\u201d 
Before we jump into that, notice that there\u2019s also another section called \u201cSites\u201d that we\u2019ve ignored 
so far. Go ahead and click on this, and you should see one site listed as Let\u2019s go ahead and 
change that information to match our own site. Click that domain name and you\u2019ll be taken to an edit 
screen where you can change the domain name and the display name of your site. Go ahead and put in 
your information, as I have in Figure 4-5. 
Figure 4-5. Editing the site domain and display name.
Now, click back to the admin interface homepage and go into the \u201cFlatpages\u201d section. Click the 
\u201cAdd Flat Page\u201d button at the top right and you\u2019ll be taken to a form that asks you for four things: a url 
where the site should reside, a title, content, and the choice of a site in a select box, which should now 
contain your own domain information. 
Enter in some values for a test page. I\u2019m going to put in my site\u2019s contact page information, as 
shown in Figure 4-6. 
Figure 4-6. Add a Contact static flatpage. 
When you\u2019re choosing URLs for your flatpages, you\u2019ll want to be careful to avoid entering URLs that 
are used by other pages. Also, make sure you select the site in the select box or else you\u2019ll get an error 
when you try to click \u201cSave.\u201d Under the \u201cAdvanced Options\u201d tab, you can specify the exact name of the 
template that will be used to render the page, but in our case, we\u2019re just going to fall back on the default 
Save this page, and you should be taken back to a page that lists your flatpages, of which there is just 
one. Feel free to create a bunch more pages for your project. 
 After you\u2019re done adding pages, go back to your project files. In your project\u2019s templates directory, 
create a subdirectory called flatpages and create a single file called default.html. This is the file that the 
flatpage module will use in rendering each page. Put the following code into the 
flatpages/default.html file: 
{% extends &quot;catalog.html&quot; %} 
{% block title %} 
 {{ flatpage.title }} - {{ site_name }} 
{% endblock %} 
{% block content %} 
 <h1>{{ flatpage.title }}</h1> 
 {{ flatpage.content }} 
{% endblock %}
As you can see, we\u2019re overriding the site title block with our own content, where we inject the page 
title manually. The content is little more than just a header with the title, and contains the content for 
this page below it. 
Navigation Tags 
While we\u2019re at all of this, let\u2019s create an inclusion tag for our footer, which will be a list of links that 
includes all of our new flatpages. Inside your project\u2019s templates/tags directory, create a file called 
footer.html and enter the following template code: 
<a href=&quot;{% url catalog_home %}&quot;>Home</a> 
{% with flatpage_list as pages %} 
 {% for page in pages %} 
 <a href=&quot;{{ page.url }}&quot;>{{ page.title }}</a> 
 {% endfor %} 
{% endwith %} 
<a href=&quot;{% url show_cart %}&quot;>Shopping Cart</a> 
This contains a link to the homepage, the shopping cart page, and between them, lists all of your 
flatpages. Then, inside your, enter the following code: 
from django.contrib.flatpages.models import FlatPage 
def footer_links(): 
 flatpage_list = FlatPage.objects.all() 
 return {'flatpage_list': flatpage_list } 
Lastly, inside your catalog.html template, change the placeholder line of text: 
[footer here] 
to this custom tag: 
{% footer_links %} 
Now we have a footer with actual navigable content that includes