本文整理汇总了Python中rango.models.Category.decode方法的典型用法代码示例。如果您正苦于以下问题:Python Category.decode方法的具体用法?Python Category.decode怎么用?Python Category.decode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rango.models.Category
的用法示例。
在下文中一共展示了Category.decode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: category
# 需要导入模块: from rango.models import Category [as 别名]
# 或者: from rango.models.Category import decode [as 别名]
def category(request, category_name_url):
# Request our context from the request passed to us.
#context = RequestContext(request)
# NOT ANYMORE WITH RENDER
# Change underscores in the category name to spaces.
# URLs don't handle spaces well, so we encode them as underscores.
# We can then simply replace the underscores with spaces again to
# get the name.
category_name = Category.decode(category_name_url)
# Create a context dictionary which we can pass to the template
# rendering engine. We start by containing the name of the
# category passed by the user.
cat_list = get_category_list()
context_dict = {'category_name': category_name, 'cat_list':
cat_list}
context_dict['category_name_url'] = category_name_url
try:
# Can we find a category with the given name?
# If we can't, the .get() method raises a DoesNotExist
# exception. So the .get() method returns one model instance
# or raises an exception.
category = Category.objects.get(name=category_name)
# Retrieve all of the associated pages.
# Note that filter returns >= 1 model instance.
pages = Page.objects.filter(category=category)
# Adds our results list to the template context under name
# pages.
context_dict['pages'] = pages
# We also add the category object from the database to the
# context dictionary. We'll use this in the template to verify
# that the category exists.
context_dict['category'] = category
#Adding likes of category to context dictionary
context_dict['category_likes'] = category.likes
except Category.DoesNotExist:
# We get here if we didn't find the specified category.
# Don't do anything - the template displays the "no category"
# message for us.
return add_category(request)
# Go render the response and return it to the client.
return render(request, 'rango/category.html', context_dict)
示例2: add_page
# 需要导入模块: from rango.models import Category [as 别名]
# 或者: from rango.models.Category import decode [as 别名]
def add_page(request, category_name_url):
category_name = Category.decode(category_name_url)
if request.method == 'POST':
form = PageForm(request.POST)
if form.is_valid():
# This time we cannot commit straight away.
# Not all fields are automatically populated!
page = form.save(commit=False)
# Retrieve the associated Category object so we can add it.
# Wrap the code in a try block - check if the category actually exists!
try:
cat = Category.objects.get(name=category_name)
page.category = cat
except Category.DoesNotExist:
# If we get here, the category does not exist.
# Go back and render the add category form as a way of saying the category does not exist.
return render(request, 'rango/add_category.html', {})
# Also, create a default value for the number of views.
page.views = 0
# With this, we can then save our new model instance.
page.save()
# Now that the page is saved, display the category instead.
return category(request, category_name_url)
else:
print(form.errors)
else:
form = PageForm()
cat_list = get_category_list()
return render(request, 'rango/add_page.html',
{'category_name_url': category_name_url,
'category_name': category_name, 'cat_list': cat_list,
'form': form})