annuaire-eleves/fiches/views.py
2020-01-15 21:27:37 +01:00

38 lines
1.2 KiB
Python

from django.shortcuts import render
from django.shortcuts import get_object_or_404,redirect
from django.contrib.auth.decorators import login_required
from fiches.models import Profile
from fiches.forms import ProfileForm, SearchForm
from django.urls import reverse
# Create your views here.
@login_required
def fiche(request,id):
profile=get_object_or_404(Profile,id=id)
return render(request,'fiches/fiche.html',{"profile":profile})
@login_required
def fiche_modif(request):
profile = request.user.profile
if request.method == 'POST':
form = ProfileForm(request.POST, instance=profile)
if form.is_valid():
form.save()
return redirect(reverse('fiche',args=(profile.id,)))
else:
form = ProfileForm(instance=profile)
return render(request,'fiches/fiches_modif.html',{"form":form})
@login_required
def search(request):
if request.method == 'POST':
form = SearchForm(request.POST)
if form.is_valid():
result = Profile.objects.filter(full_name__icontains=form.cleaned_data['name'])
return render(request,'fiches/search.html',{"form":form,"result":result})
else:
form = SearchForm()
return render(request,'fiches/search.html',{"form":form})