def listing(request):
tags = get_list_or_404(Tag)
return render(request, 'tags/listing.html', dict(
tags = tags,
))
python类get_list_or_404()的实例源码
def listing(request):
sections = get_list_or_404(Section)
return render(request, 'sections/listing.html', dict(
sections = sections,
))
def listing(request):
profiles = get_list_or_404(Profile)
return render(request, 'profiles/listing.html', dict(
profiles = profiles,
))
def listing(request, section):
posts = get_list_or_404(Post, section=section)
return render(request, 'posts/listing.html', dict(
posts = posts,
))
def listing(request, section, id, slug):
comments = get_list_or_404(Comment.objects.select_related('author', 'post', 'parent'), post=id)
return render(request, 'comments/listing.html', dict(
comments = comments,
))
def index(request):
# latest_question_list = Question.objects.order_by('-pub_date')[:5]
latest_question_list = get_list_or_404(Question.objects.order_by('-pub_date')[:5])
return render(request, 'polls/index.html', {'latest_question_list': latest_question_list})
def employee_list(request, format=None):
"""
Returns the full employee list or result list if you use ?search=
---
serializer: employees.serializers.EmployeeListSerializer
parameters:
- name: search
required: false
type: string
paramType: query
responseMessages:
- code: 401
message: Unauthorized. Authentication credentials were not provided. Invalid token.
- code: 403
message: Forbidden.
- code: 404
message: Not found
"""
if request.method == 'GET':
if request.GET.get('search'):
request_terms = request.GET.get('search')
search_terms_array = request_terms.split()
initial_term = search_terms_array[0]
employee_list = Employee.objects.filter(
Q(first_name__icontains=initial_term) |
Q(last_name__icontains=initial_term) |
Q(username__icontains=initial_term)).filter(is_active=True, is_base_profile_complete=True)
if len(search_terms_array) > 1:
for term in range(1, len(search_terms_array)):
employee_list = employee_list.filter(
Q(first_name__icontains=search_terms_array[term]) |
Q(last_name__icontains=search_terms_array[term]) |
Q(username__icontains=search_terms_array[term])).filter(
is_active=True,
is_base_profile_complete=True)
else:
employee_list = get_list_or_404(Employee, is_active=True, is_base_profile_complete=True)
paginator = PageNumberPagination()
results = paginator.paginate_queryset(employee_list, request)
serializer = EmployeeListSerializer(results, many=True)
return paginator.get_paginated_response(serializer.data)
def edu_misclassify(request, id):
instance = get_list_or_404(AllParsedProfile, id=id)
# program = instance.school_program
form = EducationMisClassify(request.POST or None)
if form.is_valid():
print(form.cleaned_data)
return redirect('home')
return render(request, 'edu_misclassify.html', {'form':form, 'education':instance})
#def ind(request):
# if request.method == 'POST':
# form = AuthorForm(request.POST)
# if form.is_valid():
# y = form.save()
# print(form.cleaned_data.get('title'))
# return redirect('home')
# else:
# form = AuthorForm()
# if request.method == 'POST':
# form = FormForm(request.POST)
# if form.is_valid():
# print(form['e'])
# return redirect('home')
# else:
# form = FormForm()
# return render(request, 'index.html', {'form': form})
#
# def signup(request):
# if request.method == 'POST':
# form = SignUpForm(request.POST)
# if form.is_valid():
# user = form.save()
# raw_password = form.cleaned_data.get('password1')
# user = authenticate(username=user.username, password=raw_password)
# login(request, user)
# return redirect('home')
# else:
# form = SignUpForm()
# return render(request, 'signup.html', {'form': form})
# def signup(request):
# if request.method == 'POST':
# form = UserCreationForm(request.POST)
# if form.is_valid():
# user = form.save()
# username = form.cleaned_data.get('username')
# raw_password = form.cleaned_data.get('password1')
# user = authenticate(username=username, password=raw_password)
# login(request, user)
# return redirect('home')
# else:
# form = UserCreationForm()
# return render(request, 'signup.html', {'form': form})