本文整理匯總了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')