kers/users/views.py

38 lines
1.1 KiB
Python
Raw Normal View History

2020-07-22 16:06:04 +02:00
from pprint import pprint
2020-07-22 05:03:01 +02:00
from django.contrib.auth import logout
2020-07-22 16:06:04 +02:00
from django.http import HttpResponseRedirect
2020-07-22 05:03:01 +02:00
from django.shortcuts import render, redirect
2020-07-22 17:33:35 +02:00
from django.urls import reverse
2020-07-22 04:21:29 +02:00
2020-07-22 16:06:04 +02:00
from users.forms import UserMetaForm
from users.models import CustomUser
2020-07-22 04:21:29 +02:00
def profile(request):
2020-07-22 17:33:35 +02:00
if not request.user.is_authenticated:
return HttpResponseRedirect(reverse("events:index"))
2020-07-22 16:06:04 +02:00
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = UserMetaForm(request.POST, instance=request.user)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
form.save()
# redirect to a new URL:
2020-07-22 17:33:35 +02:00
return HttpResponseRedirect(reverse("users:profile"))
2020-07-22 16:06:04 +02:00
# if a GET (or any other method) we'll create a blank form
else:
form = UserMetaForm(instance=request.user)
2020-07-22 05:03:01 +02:00
2020-07-22 17:33:35 +02:00
return render(request, 'users/profile.html', {'form': form})
2020-07-22 05:03:01 +02:00
def logout_view(request):
logout(request)
2020-07-22 17:33:35 +02:00
return redirect(reverse("users:profile"))