def edit(request): if request.method == 'POST': form = CustomUserChangeForm(request.POST, instance=request.user) if form.is_valid(): form.save() return redirect('index') else: form = CustomUserChangeForm(instance=request.user) context = get_index_data() context.update({ 'form': form, }) return render(request, 'accounts/form.html', context)
def password(request): if request.method == 'POST': form = PasswordChangeForm(request.user, request.POST) if form.is_valid(): form.save() update_session_auth_hash(request, form.user) return redirect('index') else: form = PasswordChangeForm(request.user) context = get_index_data() context.update({ 'form': form, }) return render(request, 'accounts/form.html', context)
def login(request): if not request.user.is_authenticated: if request.method == 'POST': form = AuthenticationForm(request, request.POST) if form.is_valid(): auth_login(request, form.get_user()) return redirect('index') else: form = AuthenticationForm() context = get_index_data() context.update({ 'form': form, }) return render(request, 'accounts/form.html', context) else: return redirect('index')
def register(request): if not request.user.is_authenticated: if request.method == 'POST': form = CustomUserCreationForm(request.POST) if form.is_valid(): user = form.save() auth_login(request, user) return redirect('index') else: form = CustomUserCreationForm() context = get_index_data() context.update({ 'form': form, }) return render(request, 'accounts/form.html', context) else: return redirect('index')
def profile(request, username): user = get_object_or_404(User, username=username) # pagination - my_articles my_articles = user.my_articles.all() paginator_my = Paginator(my_articles, 5) page_number_my = request.GET.get('page_my') page_obj_my = paginator_my.get_page(page_number_my) # pagination - pinned_articles pinned_articles = user.pinned_articles.all() paginator_pin = Paginator(pinned_articles, 5) page_number_pin = request.GET.get('page_pin') page_obj_pin = paginator_pin.get_page(page_number_pin) context = get_index_data() context.update({ 'user': user, 'page_obj_my': page_obj_my, 'page_obj_pin': page_obj_pin, }) return render(request, 'accounts/profile.html', context)