本文整理汇总了Python中django.shortcuts.get_list_or_404方法的典型用法代码示例。如果您正苦于以下问题:Python shortcuts.get_list_or_404方法的具体用法?Python shortcuts.get_list_or_404怎么用?Python shortcuts.get_list_or_404使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.shortcuts
的用法示例。
在下文中一共展示了shortcuts.get_list_or_404方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: blog_search
# 需要导入模块: from django import shortcuts [as 别名]
# 或者: from django.shortcuts import get_list_or_404 [as 别名]
def blog_search(request,):
search_for = request.GET['search_for']
if search_for:
results = []
article_list = get_list_or_404(Article)
category_list = get_list_or_404(Category)
for article in article_list:
if re.findall(search_for, article.title):
results.append(article)
for article in results:
article.body = markdown.markdown(article.body, )
tag_list = Tag.objects.all().order_by('name')
return render(request, 'blog/search.html', {'article_list': results,
'category_list': category_list,
'tag_list': tag_list})
else:
return redirect('app:index')
示例2: get_users_and_groups
# 需要导入模块: from django import shortcuts [as 别名]
# 或者: from django.shortcuts import get_list_or_404 [as 别名]
def get_users_and_groups(resource_permission):
group_popover = OrderedDict()
inactive_user = {str(user.username) for user in models.User.objects.filter(is_active=False)}
superusers_set = {str(user.username) for user in models.User.objects.filter(is_superuser=True)}
try:
group_resource_permission_list = get_list_or_404(models.GroupResourcePermission,
resource_permission=resource_permission)
groups_list = [str(obj.group.name) for obj in group_resource_permission_list]
for obj in groups_list:
users_set = {str(user.username) for user in get_user_model().objects.filter(groups__name=obj)}
users_list = sorted(users_set - inactive_user)
group_popover.setdefault('@' + obj, users_list)
except Http404:
pass
group_popover.setdefault('@superusers', sorted(superusers_set - inactive_user))
return group_popover
示例3: preview_collection
# 需要导入模块: from django import shortcuts [as 别名]
# 或者: from django.shortcuts import get_list_or_404 [as 别名]
def preview_collection(request, slug):
acitvities = [
{
'url': (
f'{reverse("lti:source-preview")}?source_id={a.id}&source_name={urllib.parse.quote_plus(a.name)}'
f'&source_lti_url={a.source_launch_url}&content_source_id={a.lti_content_source_id}'
),
'pos': pos,
}
for pos, a in enumerate(get_list_or_404(Activity, collection__slug=slug), start=1)
]
return render(
request,
template_name="module/sequence_preview.html",
context={
'activities': acitvities,
'back_url': (
f"{reverse('module:collection-detail', kwargs={'slug': slug})}"
f"?back_url={request.GET.get('back_url')}"
)
}
)
示例4: test_bad_class
# 需要导入模块: from django import shortcuts [as 别名]
# 或者: from django.shortcuts import get_list_or_404 [as 别名]
def test_bad_class(self):
# Given an argument klass that is not a Model, Manager, or Queryset
# raises a helpful ValueError message
msg = "First argument to get_object_or_404() must be a Model, Manager, or QuerySet, not 'str'."
with self.assertRaisesMessage(ValueError, msg):
get_object_or_404("Article", title__icontains="Run")
class CustomClass:
pass
msg = "First argument to get_object_or_404() must be a Model, Manager, or QuerySet, not 'CustomClass'."
with self.assertRaisesMessage(ValueError, msg):
get_object_or_404(CustomClass, title__icontains="Run")
# Works for lists too
msg = "First argument to get_list_or_404() must be a Model, Manager, or QuerySet, not 'list'."
with self.assertRaisesMessage(ValueError, msg):
get_list_or_404([Article], title__icontains="Run")
示例5: _get_faculty_or_404
# 需要导入模块: from django import shortcuts [as 别名]
# 或者: from django.shortcuts import get_list_or_404 [as 别名]
def _get_faculty_or_404(allowed_units, userid_or_emplid):
"""
Get the Person who has Role[role=~"faculty"] if we're allowed to see it, or raise Http404.
"""
sub_unit_ids = Unit.sub_unit_ids(allowed_units)
person = get_object_or_404(Person, find_userid_or_emplid(userid_or_emplid))
roles = get_list_or_404(Role, role='FAC', unit__id__in=sub_unit_ids, person=person)
units = set(r.unit for r in roles)
return person, units
示例6: get
# 需要导入模块: from django import shortcuts [as 别名]
# 或者: from django.shortcuts import get_list_or_404 [as 别名]
def get(self, request):
empty_qs = APIRequestLog.objects.none()
return get_list_or_404(empty_qs)
示例7: get_queryset
# 需要导入模块: from django import shortcuts [as 别名]
# 或者: from django.shortcuts import get_list_or_404 [as 别名]
def get_queryset(self):
self.tag = get_object_or_404(Tags, slug=self.kwargs.get("tag_slug"))
return get_list_or_404(Post, tags=self.tag, status='Published', category__is_active=True)
示例8: get_queryset
# 需要导入模块: from django import shortcuts [as 别名]
# 或者: from django.shortcuts import get_list_or_404 [as 别名]
def get_queryset(self):
# 重写通用视图的 get_queryset 函数,获取定制数据
queryset = super(IndexView, self).get_queryset()
# 日期归档
year = self.kwargs.get('year', 0)
month = self.kwargs.get('month', 0)
# 标签
tag = self.kwargs.get('tag', 0)
# 导航条
self.big_slug = self.kwargs.get('bigslug', '')
# 文章分类
slug = self.kwargs.get('slug', '')
if self.big_slug:
big = get_object_or_404(BigCategory, slug=self.big_slug)
queryset = queryset.filter(category__bigcategory=big)
if slug:
slu = get_object_or_404(Category, slug=slug)
queryset = queryset.filter(category=slu)
if year and month:
queryset = get_list_or_404(queryset, create_date__year=year, create_date__month=month)
if tag:
tags = get_object_or_404(Tag, name=tag)
self.big_slug = BigCategory.objects.filter(category__article__tags=tags)
self.big_slug = self.big_slug[0].slug
queryset = queryset.filter(tags=tags)
return queryset
示例9: test_get_list_or_404_queryset_attribute_error
# 需要导入模块: from django import shortcuts [as 别名]
# 或者: from django.shortcuts import get_list_or_404 [as 别名]
def test_get_list_or_404_queryset_attribute_error(self):
"""AttributeError raised by QuerySet.filter() isn't hidden."""
with self.assertRaisesMessage(AttributeError, 'AttributeErrorManager'):
get_list_or_404(Article.attribute_error_objects, title__icontains='Run')