GeezerVison is showing github.com — add index · Xu-Guo/python@68b50da
File treeExpand file treeCollapse file tree Open diff view settings tango_with_django_project
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard Collapse file Copy file name to clipboardExpand all lines: tango_with_django_project/populate_rango2.py +16-8Lines changed: 16 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,39 +12,47 @@ def populate(): | 12 | 12 |
| 13 | 13 | add_page(c_id=3, | 14 | 14 | title="Official Python Tutorial", | 15 | | - url="http://docs.python.org/2/tutorial/") | | 15 | + url="http://docs.python.org/2/tutorial/", | | 16 | + views=800) | 16 | 17 |
| 17 | 18 | add_page(c_id=3, | 18 | 19 | title="How to Think like a Computer Scientist", | 19 | | - url="http://www.greenteapress.com/thinkpython/") | | 20 | + url="http://www.greenteapress.com/thinkpython/", | | 21 | + views=700) | 20 | 22 |
| 21 | 23 | add_page(c_id=3, | 22 | 24 | title="Learn Python in 10 Minutes", | 23 | | - url="http://www.korokithakis.net/tutorials/python/") | | 25 | + url="http://www.korokithakis.net/tutorials/python/", | | 26 | + views=600) | 24 | 27 |
| 25 | 28 | django_cat = add_cat("Django", 64, 32) | 26 | 29 |
| 27 | 30 | add_page(c_id=4, | 28 | 31 | title="Official Django Tutorial", | 29 | | - url="https://docs.djangoproject.com/en/1.5/intro/tutorial01/") | | 32 | + url="https://docs.djangoproject.com/en/1.5/intro/tutorial01/", | | 33 | + views=1000) | 30 | 34 |
| 31 | 35 | add_page(c_id=4, | 32 | 36 | title="Django Rocks", | 33 | | - url="http://www.djangorocks.com/") | | 37 | + url="http://www.djangorocks.com/", | | 38 | + views=500) | 34 | 39 |
| 35 | 40 | add_page(c_id=4, | 36 | 41 | title="How to Tango with Django", | 37 | | - url="http://www.tangowithdjango.com/") | | 42 | + url="http://www.tangowithdjango.com/", | | 43 | + views=900) | 38 | 44 |
| 39 | 45 | frame_cat = add_cat("Other Frameworks", 32, 16) | 40 | 46 |
| 41 | 47 | add_page(c_id=5, | 42 | 48 | title="Bottle", | 43 | | - url="http://bottlepy.org/docs/dev/") | | 49 | + url="http://bottlepy.org/docs/dev/", | | 50 | + views=400) | 44 | 51 |
| 45 | 52 | add_page(c_id=5, | 46 | 53 | title="Flask", | 47 | | - url="http://flask.pocoo.org") | | 54 | + url="http://flask.pocoo.org", | | 55 | + views=300) | 48 | 56 |
| 49 | 57 | # Print out what we have added to the user. | 50 | 58 | for c in Category.objects.all(): | |
Collapse file Copy file name to clipboardExpand all lines: tango_with_django_project/rango/views.py +9-4Lines changed: 9 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | # Create your views here. | 7 | 7 | from django.http import HttpResponse | 8 | 8 | # Import the Category model | 9 | | -from rango.models import Category | | 9 | +from rango.models import Category, Page | 10 | 10 | import logging | 11 | 11 | logging.basicConfig(level=logging.DEBUG) | 12 | 12 |
| @@ -29,8 +29,12 @@ def index(request): | 29 | 29 | # Order the categories by no. likes in descending order. | 30 | 30 | # Retrieve the top 5 only - or all if less than 5. | 31 | 31 | # Place the list in our context_dict dictionary which will be passed to the template engine. | | 32 | + context_dict = {} | 32 | 33 | category_list = Category.objects.order_by('-likes')[:5] | 33 | | - context_dict = {'categories': category_list} | | 34 | + context_dict['categories'] = category_list | | 35 | + | | 36 | + page_list = Page.objects.order_by('-views')[:5] | | 37 | + context_dict['pages'] = page_list | 34 | 38 |
| 35 | 39 | # Render the response and send it back! | 36 | 40 | return render(request, 'rango/index.html', context_dict) | @@ -43,7 +47,7 @@ def about(request): | 43 | 47 |
| 44 | 48 |
| 45 | 49 | def category(request, category_name_slug): | 46 | | - logging.debug("category_name_slug:{0}".format(category_name_slug)) | | 50 | + logging.debug("category_name_slug-------------{0}".format(category_name_slug)) | 47 | 51 | # Create a context dictionary which we can pass to the template rendering engine. | 48 | 52 | context_dict = {} | 49 | 53 |
| @@ -64,7 +68,8 @@ def category(request, category_name_slug): | 64 | 68 | # We'll use this in the template to verify that the category exists. | 65 | 69 | context_dict['category'] = category | 66 | 70 |
| 67 | | - logging.debug('pages:{0}'.format) | | 71 | + logging.debug('pages-------------{0}'.format(pages)) | | 72 | + logging.debug('category-------------{0}'.format(category)) | 68 | 73 |
| 69 | 74 | except Category.DoesNotExist: | 70 | 75 | # We get here if we didn't find the specified category. | |
Collapse file Copy file name to clipboard +29-18Lines changed: 29 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | <!DOCTYPE html> | 2 | 2 | <html> | | 3 | + <head> | | 4 | + <title>Rango</title> | | 5 | + </head> | 3 | 6 |
| 4 | | -<head> | 5 | | - <title>Rango</title> | 6 | | -</head> | | 7 | + <body> | | 8 | + <h1>Rango says..hello world!</h1> | 7 | 9 |
| 8 | | -<body> | 9 | | - <h1>Rango says...hello world!</h1> | | 10 | + <h2>Top 5 Category</h2> | | 11 | + {% if categories %} | | 12 | + <ul> | | 13 | + {% for category in categories %} | | 14 | + <!-- Following line changed to add an HTML hyperlink --> | | 15 | + <li><a href="/rango/category/{{ category.slug }}">{{ category.name }}</a></li> | | 16 | + {% endfor %} | | 17 | + </ul> | | 18 | + {% else %} | | 19 | + <strong>There are no categories present.</strong> | | 20 | + {% endif %} | 10 | 21 |
| 11 | | - {% if categories %} | 12 | | - <ul> | 13 | | - {% for category in categories %} | 14 | | - <li>{{ category.name }}</li> | 15 | | - {% endfor %} | 16 | | - </ul> | 17 | | - {% else %} | 18 | | - <strong>There are no categories present.</strong> | 19 | | - {% endif %} | 20 | | - | 21 | | - <a href="/rango/about/">About</a> | 22 | | -</body> | | 22 | + <h2>Top 5 Page</h2> | | 23 | + {% if pages %} | | 24 | + <ul> | | 25 | + {% for page in pages %} | | 26 | + <li>{{ page.title }}</li> | | 27 | + {% endfor %} | | 28 | + </ul> | | 29 | + {% else %} | | 30 | + <strong>There are no pages present.</strong> | | 31 | + {% endif %} | 23 | 32 |
| 24 | | -</html> | | 33 | + | | 34 | + </body> | | 35 | +</html> |
|