本文整理汇总了Python中models.Category._encode_url_name方法的典型用法代码示例。如果您正苦于以下问题:Python Category._encode_url_name方法的具体用法?Python Category._encode_url_name怎么用?Python Category._encode_url_name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Category
的用法示例。
在下文中一共展示了Category._encode_url_name方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: index
# 需要导入模块: from models import Category [as 别名]
# 或者: from models.Category import _encode_url_name [as 别名]
def index(request):
"""
Query the database for a list of ALL categories currently stored
Order the categories by "likes" in descending order
Retrieve the top 5 only - or all if less than 5
Place the list in dictionary:context_dict and pass to the template engine: index.html
:param request:
:return:
"""
# GET category list, sort by "likes" in descending order -> order_by("-likes")
# For each category object returned, add attribute "url", replace " " with "_"
category_list = Category.objects.order_by("-likes")[:5]
for a_category in category_list[:5]:
if not hasattr(category,"url"):
setattr(a_category,"url", Category._encode_url_name(a_category.name))
# GET page list, sort descending by "views", just the top 5
page_list = Page.objects.order_by("-views")[:5]
# Construct a dictionary to pass to the template engine as it's context
# Note the key boldmessage is the same as {{ boldmessage }} in the template: rango/index.html
context_dict = {
"boldmessage": "You are welcome to register or login",
"categories": category_list,
"pages": page_list,
}
return _process_request(request, context_dict, r"rango/index.html")
示例2: add_category
# 需要导入模块: from models import Category [as 别名]
# 或者: from models.Category import _encode_url_name [as 别名]
def add_category(request):
"""
Either show a form or create a new instance of one.
"""
# Is request HTTP POST or GET?
if request.method == "POST":
form = CategoryForm(request.POST)
if form.is_valid():
category = form.save(commit=False)
setattr(category,"url", Category._encode_url_name(category.name))
category.save()
# now call the index() view
# user redirected to home page
return index(request)
else:
# Uh oh, supplied form contains error - print out to console
print form.errors
else:
# request not a POST, display the form to enter details
form = CategoryForm()
# Bad form (or form details), no form supplied...
# Render the form with error messages (if any).
return _process_request(request, {"form": form}, "rango/add_category.html")
示例3: add_page
# 需要导入模块: from models import Category [as 别名]
# 或者: from models.Category import _encode_url_name [as 别名]
def add_page(request, category_name_url):
"""
Add page to category if it doesn't exist,
:param request
:param category_name_url
"""
category_name = Category._encode_url_name(category_name_url, Category._url_decode_rules_dict)
# category_name = "ooops"
cat = None
if request.method == "POST":
form = PageForm(request.POST)
if form.is_valid():
# Cannot commit straight away, not all fields automatically populated
page = form.save(commit=False)
# Retrieve the associated Category object so we can add it
# Handle exception for Model.DoesNotExist, Go back and render the add category form
try:
cat = Category.objects.get(url=category_name_url)
page.category = cat
except Category.DoesNotExist:
# Category does not exist, Go back and the render the add_category form
return _process_request(request, {}, "rango/add_category.html")
# set default value for number of views, and save new model instance
page.views = 0
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()
context_dict = {
"category_name_url": category_name_url,
"category_name": category_name,
"form": form,
}
return _process_request(request, context_dict, "rango/add_page.html")